標籤:android自訂控制項 群組控制項
第一步:設定控制項需要的屬性
在value目錄下面建立一個attrs.xml屬性檔案
然後在裡面設定控制項屬性,首先設定控制項名稱,name裡面即你自訂的控制項名稱
<declare-styleablename="FanTitle"> </declare-styleable>
這步完成之後,在裡面為你的控制項添加屬性:
name是屬性名稱,format是屬性類型
<declare-styleablename="FanTitle"> <attr name="title"format="string"/> <attrname="titileTextSize" format="dimension"/> <attrname="titleTextColor" format="color"/> <attr name="leftText"format="string"/> <attrname="leftBackground" format="reference|color"/> <attr name="leftTextColor"format="color"/> <attr name="rightText"format="string"/> <attrname="rightBackground" format="reference|color"/> <attrname="rightTextColor" format="color"/></declare-styleable>
最外層的是控制項屬性名稱,裡面就是控制項的各個屬性,屬性的話還需要指定它的屬性類型,具體有哪些類型,可以參考我的另外一篇文章,裡面說的很詳細:http://blog.csdn.net/fanxl10/article/details/41316013
這樣設定完畢之後進行第二步操作
第二步:建立自訂View對象
packagecom.fanxl.fxlcustomview.widght; importcom.fanxl.fxlcustomview.R; importandroid.annotation.SuppressLint;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.drawable.Drawable;importandroid.util.AttributeSet;importandroid.view.Gravity;importandroid.widget.Button;importandroid.widget.RelativeLayout;importandroid.widget.TextView; /** * 自訂控制項——組合模式 * @author fanxl * */public class FanTitleextends RelativeLayout{ //裡面的控制項元素privateButton leftbt, rightbt;privateTextView title; //自訂的控制項屬性privateint leftTextColor;privateDrawable leftBackground;privateString leftText; privateint rightTextColor;privateDrawable rightBackground;privateString rightText; privatefloat titleTextSize;privateint titleTextColor;privateString titleText; //控制項位置資訊參數privateLayoutParams leftParams, rightParams, titleParams; @SuppressLint("NewApi")public FanTitle(Context context, AttributeSet attrs) {super(context,attrs); //讀取xml中自訂的控制項屬性TypedArrayta = context.obtainStyledAttributes(attrs, R.styleable.FanTitle);leftTextColor= ta.getColor(R.styleable.FanTitle_leftTextColor, 0);leftBackground= ta.getDrawable(R.styleable.FanTitle_leftBackground);leftText= ta.getString(R.styleable.FanTitle_leftText); rightTextColor= ta.getColor(R.styleable.FanTitle_rightTextColor, 0);rightBackground= ta.getDrawable(R.styleable.FanTitle_rightBackground);rightText= ta.getString(R.styleable.FanTitle_rightText); titleTextColor= ta.getColor(R.styleable.FanTitle_titleTextColor, 0);titleTextSize= ta.getDimension(R.styleable.FanTitle_titileTextSize, 0);titleText= ta.getString(R.styleable.FanTitle_title); //記得回收ta.recycle(); //建立裡面的控制項leftbt= new Button(context);rightbt= new Button(context);title= new TextView(context); //設定讀取出來的控制項屬性leftbt.setText(leftText);leftbt.setBackground(leftBackground);leftbt.setTextColor(leftTextColor); rightbt.setText(rightText);rightbt.setBackground(rightBackground);rightbt.setTextColor(rightTextColor); title.setText(titleText);title.setTextColor(titleTextColor);title.setTextSize(titleTextSize);title.setGravity(Gravity.CENTER); //設定自訂控制項的預設背景顏色setBackgroundColor(0xFFF59563); //設定位置資訊leftParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);addView(leftbt,leftParams); rightParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);addView(rightbt,rightParams); titleParams= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);addView(title,titleParams);} }
第三步:XML布局中引用自訂控制項
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" xmlns:fxl="http://schemas.android.com/apk/res/com.fanxl.fxlcustomview" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <com.fanxl.fxlcustomview.widght.FanTitle android:layout_width="match_parent" android:layout_height="40dp" fxl:title="我的空間" fxl:titileTextSize="16sp" fxl:titleTextColor="#ff0000" fxl:leftText="返回" fxl:leftBackground="#e0e0e0" fxl:leftTextColor="#000000" fxl:rightText="更多" fxl:rightBackground="#e9e9e9" fxl:rightTextColor="#0000ff"> </com.fanxl.fxlcustomview.widght.FanTitle> </RelativeLayout>
需要注意,要使用我們自己的命名空間:
xmlns:fxl="http://schemas.android.com/apk/res/com.fanxl.fxlcustomview"
第四步:擴充和完善我們的自訂控制項
設定完畢之後的效果如下,這個時候我們是不能點擊兩邊的按鈕,還需要完善自訂控制項
想要實現上面的需求,就需要添加回呼函數:
privateFanTitleClickListener clickListener; //添加介面回調方法供外部使用public interfaceFanTitleClickListener{publicvoid leftClick();publicvoid rightClick();} public voidsetOnTitleClickListener(FanTitleClickListener clickListener){this.clickListener=clickListener;}給按鈕添加點擊事件:leftbt.setOnClickListener(newOnClickListener() { @Overridepublicvoid onClick(View v) {clickListener.leftClick();}}); rightbt.setOnClickListener(newOnClickListener() { @Overridepublicvoid onClick(View v) {clickListener.rightClick();}});
最後,在外部實現這個回呼函數,並在函數中實現具體的操作:
FanTitle main_title =(FanTitle) findViewById(R.id.main_title);main_title.setOnTitleClickListener(newFanTitleClickListener() { @Overridepublicvoid rightClick() {Toast.makeText(MainActivity.this, "右邊點擊了", Toast.LENGTH_SHORT).show();} @Overridepublicvoid leftClick() {Toast.makeText(MainActivity.this, "左邊點擊了", Toast.LENGTH_SHORT).show();}});
到此,我們點擊按鈕就會有提示資訊。
除了這些,我們還可以針對自己的需要來完善這個組件,比如,可以隱藏左右邊的按鈕,那麼只需要在控制項內部提供方法供外部設定即可,或者在xml檔案中添加屬性,這些都是可以的。
android組合自訂控制項