1.在attr.xml文件中设计需要的属性
<declare-styleable name="Topbar"> <attr name="title" format="string"/> <attr name="titleTextSize" format="dimension"/> <attr name="titleTextColor" format="color"/> <attr name="leftText" format="string"/> <attr name="leftTextColor" format="color"/> <attr name="leftBackground" format="reference|color"/> <attr name="rightText" format="color"/> <attr name="rightTextColor" format="color"/> <attr name="rightBackground" format="reference|color"/> </declare-styleable>
2.在类中实现View
a.
TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.Topbar);
获取在布局中通过TypedArray储存对自定义View设置的值,
b.
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.getDimensionPixelSize(R.styleable.Topbar_titleTextSize,0); titleTextColor = ta.getColor(R.styleable.Topbar_leftTextColor,0); title = ta.getString(R.styleable.Topbar_title);获取所有的属性值
c.
ta.recycle();
清空TypedArray所占用的资源,释放内存
d.
leftButton = new Button(context); rightButton = new Button(context); tvTitle = new TextView(context); leftButton.setTextColor(leftTextColor); leftButton.setBackground(leftBackground); leftButton.setText(leftText); rightButton.setTextColor(rightTextColor); rightButton.setBackground(rightBackground); rightButton.setText(rightText); tvTitle.setTextColor(titleTextColor); tvTitle.setTextSize(titleTextSize); tvTitle.setText(title); tvTitle.setGravity(Gravity.CENTER);//设置文字居中显示 setBackgroundColor(0xFFF59563);对自定义View内的组合控件进行赋值,值就是b拿到的值
e.
leftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE); addView(leftButton,leftParams);//将leftButton以leftParams的方式放到布局中 rightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE); addView(rightButton,rightParams);//将rightButton以rightParams的方式放到布局中 titleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT); titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE); addView(tvTitle,titleParams);让控件以设定的LayoutParams方式显示到布局中
3.在布局文件中应用自定义的View
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.mytopbar.MainActivity"> <com.example.mytopbar.TopBar android:id="@+id/topbar" android:layout_width="match_parent" android:layout_height="50dp" custom:leftBackground="#FFCCCC" custom:leftTextColor="#000000" custom:leftText="Back" custom:rightBackground="#FFCCCC" custom:rightTextColor="#000000" custom:rightText="More" custom:title="自定义标题" custom:titleTextColor="#33CCFF" custom:titleTextSize="10sp"></com.example.mytopbar.TopBar>重点是这个
xmlns:custom="http://schemas.android.com/apk/res-auto"topbar引用这里引入的资源,就相当于java文件中的import