首先在values下新建一个atts文件夹,添加
declare-styleable
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="Topbar"> <attr name="title" format="string"></attr> <attr name="titleTextSize" format="dimension"></attr> <attr name="titleTextColor" format="color"></attr> <attr name="leftTextColor" format="color"></attr> <attr name="leftBackground" format="color|reference"></attr> <attr name="leftText" format="string"></attr> <attr name="rightText" format="string"></attr> <attr name="rightBackground" format="color|reference"></attr> <attr name="rightTextColor" format="color"></attr> </declare-styleable> </resources>
XML布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.example.hc.mytopbar.TopBar android:id="@+id/tp" android:layout_width="wrap_content" android:layout_height="wrap_content" custom:leftText="back" custom:leftTextColor="#ffff00" custom:rightTextColor="#ffff00" custom:rightText="menu" custom:title="菜单" custom:titleTextColor="#123412" custom:titleTextSize="10sp"> </com.example.hc.mytopbar.TopBar> </RelativeLayout>
然后在自定义View类中的构造函数中 通过TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); 获得这个atts文件
获得atts中自定义属性之后 将属性与相应的控件绑定public class TopBar extends RelativeLayout { private Button leftButton, rightButton; private TextView tv_title; private int leftTextColor; private Drawable leftBackground; private String leftText; private int rightTextColor; private Drawable rightBackground; private String rightText; private float titleTextSize; private int titleTextColor; private String title; private LayoutParams leftParams, rightParams, titleParam; private topbarClickListener listener; public interface topbarClickListener { public void leftClick(); public void rightClick(); } public void setOnTopbarClickListener(topbarClickListener listner) { this.listener = listner; } public TopBar(final Context context, AttributeSet attrs) { super(context, attrs); TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Topbar); leftTextColor = ta.getColor(R.styleable.Topbar_leftTextColor, 0); leftBackground = ta.getDrawable(R.styleable.Topbar_leftBackground); leftText = ta.getString(R.styleable.Topbar_leftText); rightTextColor = ta.getColor(R.styleable.Topbar_rightTextColor, 0); rightBackground = ta.getDrawable(R.styleable.Topbar_rightBackground); rightText = ta.getString(R.styleable.Topbar_rightText); titleTextSize = ta.getDimension(R.styleable.Topbar_titleTextSize, 0); titleTextColor = ta.getColor(R.styleable.Topbar_titleTextColor, 0); title = ta.getString(R.styleable.Topbar_title); ta.recycle(); setBackgroundColor(0xfff59563); leftButton = new Button(context); rightButton = new Button(context); tv_title = new TextView(context); leftButton.setTextColor(leftTextColor); leftButton.setText(leftText); leftButton.setBackground(leftBackground); rightButton.setText(rightText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackground); tv_title.setTextColor(titleTextColor); tv_title.setTextSize(titleTextSize); tv_title.setText(title); tv_title.setGravity(Gravity.CENTER); leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); leftParams.addRule(ALIGN_PARENT_LEFT, TRUE); addView(leftButton, leftParams); rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); rightParams.addRule(ALIGN_PARENT_RIGHT, TRUE); addView(rightButton, rightParams); titleParam = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); titleParam.addRule(CENTER_IN_PARENT); addView(tv_title, titleParam);