zoukankan      html  css  js  c++  java
  • 自定义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 }
  • 相关阅读:
    腾讯课堂——基础数据类型(dict字典)
    腾讯课堂——基础数据类型(tuple元祖)
    基础数据类型(list列表)
    第 018讲:函数:灵活即强大(关键字函数,默认函数,收集函数)
    第 015讲:字符串:格式化
    第 013讲: 元组tuple 上了枷锁的列表
    第 012讲:打了激素的数组3
    第 011讲:一个打了激素的数组[02]
    range函数的用法
    第 010讲:一个打了激素的数组[01]
  • 原文地址:https://www.cnblogs.com/Sunnor/p/4962817.html
Copyright © 2011-2022 走看看