• 自定义view


    自定义view:

     1 package com.wyl.viewdefine;
     2 
     3 import android.content.Context;
     4 import android.content.res.TypedArray;
     5 import android.graphics.drawable.Drawable;
     6 import android.util.AttributeSet;
     7 import android.view.Gravity;
     8 import android.view.ViewGroup;
     9 import android.widget.Button;
    10 import android.widget.RelativeLayout;
    11 import android.widget.TextView;
    12 
    13 import com.wyl.account.R;
    14 
    15 public class TopBar extends RelativeLayout {
    16     private Button leftButton, rightButton;
    17     private TextView tvTitle;
    18 
    19     private int leftTextColor;
    20     private Drawable leftBackground;
    21     private String leftText;
    22 
    23     private int rightTextColor;
    24     private Drawable rightBackground;
    25     private String rightText;
    26 
    27     private float titleTextSize;
    28     private int titleTextColor;
    29     private String title;
    30 
    31     private LayoutParams leftParams, rightParmas, tilteParams;
    32 
    33     // private
    34 
    35     public TopBar(Context context, AttributeSet attrs) {
    36         super(context, attrs);
    37         // TODO Auto-generated constructor stub
    38         TypedArray ta = context.obtainStyledAttributes(attrs,
    39                 R.styleable.Topbar);
    40 
    41         leftText = ta.getString(R.styleable.Topbar_leftText);
    42         leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0);
    43         leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground);
    44 
    45         rightText = ta.getString(R.styleable.Topbar_leftText);
    46         rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0);
    47         rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground);
    48 
    49         title = ta.getString(R.styleable.Topbar_title);
    50         titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0);
    51         titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0);
    52 
    53         ta.recycle();
    54         /**
    55          * 获取自定义控件
    56          */
    57         leftButton = new Button(context);
    58         rightButton = new Button(context);
    59         tvTitle = new TextView(context);
    60 
    61         /**
    62          * 设置颜色等
    63          */
    64         leftButton.setTextColor(leftTextColor);
    65         leftButton.setBackground(leftBackground);
    66         leftButton.setText(leftText);
    67 
    68         rightButton.setTextColor(rightTextColor);
    69         rightButton.setBackground(rightBackground);
    70         rightButton.setText(rightText);
    71 
    72         tvTitle.setTextColor(titleTextColor);
    73         tvTitle.setTextSize(titleTextSize);
    74         tvTitle.setText(title);
    75         tvTitle.setGravity(Gravity.CENTER);
    76 
    77         setBackgroundColor(0xFFF59563);
    78 
    79         leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
    80                 ViewGroup.LayoutParams.WRAP_CONTENT);
    81 
    82         leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);
    83 
    84         addView(leftButton, leftParams);
    85 
    86     }
    87 
    88 }
  • 相关阅读:
    sgg_3_hibernate详解
    sgg_2_hibernate开发步骤
    sgg_1_hibernate概述
    cookie
    exchange 普通用户可以创建通讯组
    DC 维护常用工具命令DCdiag
    cp 复制不覆盖
    powershell 结果输出显示为……
    获得用户完整的autodiscover配置文件
    【转】outlook 2016 配置自动发现
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4962817.html
Copyright © 2020-2023  润新知