自定义view

本文介绍了一个自定义的Android视图组件TopBar,该组件通过继承RelativeLayout并利用属性集来实现高度定制化的顶部栏,包括左侧按钮、右侧按钮及标题等元素。

自定义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 }

 

转载于:https://www.cnblogs.com/Sunnor/p/4962817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值