自定義控件
一、引入布局
chuangjianyi8ge標題欄布局只需要加入兩個Button和TextView,然後在布局中擺放好就可以了。可是這樣做卻存在一個問題,一般我們的程序中可能有很多活動都需要這樣的標題欄,如果在每個活動的布局中都編寫一遍同樣的代碼,明顯會導致大量的代碼重復,這時可以使用引用布局的方式來解決這個問題
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4android:layout_height="wrap_content" 5 > 6 7 <Button 8 android:id="@+id/layout_diy_button1" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_gravity="center" 12 android:layout_margin="5dip" 13android:background="@drawable/white" 14 android:text="Back" /> 15 16 <TextView 17 android:id="@+id/layout_diy_textView1" 18 android:layout_width="0dp" 19 android:layout_height="wrap_content" 20 android:layout_weight="1" 21 android:layout_gravity="center" 22android:gravity="center" 23 android:background="@drawable/white" 24 android:textSize="24sp" 25 android:text="Title Text" 26 /> 27 28 <Button 29 android:id="@+id/layout_diy_button2" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" 32 android:layout_margin="5dip" 33 android:background="@drawable/white" 34 android:text="Edit" /> 35 36 </LinearLayout>
android:layout_margin可以指定此控件距離父View的上下左右距離,如果要單獨指定距離父View在某個方向上的偏移距離的話可以使用layout_marginLeft,layout_marginRight
,標題欄布局已經寫好了,現在就是如何在程序中使用這個標題欄了
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 <include layout="@layout/layout_diy"/> 6 7 8 </LinearLayout>
只需要一行<include layout="@layout/xxxx"/>就可以將標題欄導入了,但是別忘了在MainActivity中把系統自帶的標題欄隱藏掉,代碼如下
1 @Override 2 protected void onCreate(Bundle savedInstanceState) { 3 4 super.onCreate(savedInstanceState); 5 requestWindowFeature(Window.FEATURE_NO_TITLE); 6 setContentView(R.layout.activity_main); 7 } 8
使用這種方式,不論多少布局使用標題欄,只需要一行include語句就可以了。
二、創建自定義控件
引入布局雖然解決了編寫重復布局代碼的問題,但是如果布局中的控件需要有響應事件,還是需要在不同activity中編寫重復的響應事件代碼,這種情況最好是使用自定義控件的方式來解決。
新建一個TitleLayout,讓它繼承自LinearLayout
首先我們重寫了LinearLayout 中的帶有兩個構造參數的構造函數,在TitleLayout控件就會調用這個構造函數,然後在構造函數中需要對標題欄布局進行動態加載,這就要借助LayoutInflater來實現的。通過LayoutInflaterduixiang ,然後調用Infalter()方法就可以動態加載一個布局文件,inflater()方法接受兩個參數,第一個參數是要加載的布局文件的id,這裏我們傳入R.layout.layout_diy,第二個參數是給加載好的布局再添加一個父布局,這裏我們想要指定為TileLayout,於是直接傳入this
1 public class TitleLayout extends LinearLayout{ 2 3 public TitleLayout(Context context, AttributeSet attrs) 4 { 5 super(context, attrs); 6 // TODO Auto-generated constructor stub 7 LayoutInflater.from(context).inflate(R.layout.layout_diy_test , this); 8 } 9 }
現在自定義控件已經創建好了,然後我們需要在布局文件中添加這個自定義控件,修改activity_main.xml中的代碼,如下所示:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 android:layout_width="match_parent" 3 android:layout_height="match_parent" 4 android:orientation="vertical" > 5 6 <com.example.mm.TitleLayout 7 android:layout_width="match_parent" 8 android:layout_height="wrap_content" > 9 </com.example.mm.TitleLayout> 10 11 </LinearLayout>
重新運行程序,你會發現此時效果和使用引入布局方式的效果是一樣
然後嘗試為標題欄中的按鈕註冊點擊事件,修改TitleLayout中的代碼,如圖
1 public class TitleLayout extends LinearLayout{ 2 3 public TitleLayout(Context context, AttributeSet attrs) 4 { 5 super(context, attrs); 6 // TODO Auto-generated constructor stub 7 LayoutInflater.from(context).inflate(R.layout.layout_diy_test , this); 8 Button titleBack=(Button)findViewById(R.id.layout_diy_button1); 9 Button titleEdit=(Button)findViewById(R.id.layout_diy_button2); 10 titleBack.setOnClickListener(new OnClickListener(){ 11 12 @Override 13 public void onClick(View v) { 14 15 // TODO Auto-generated method stub 16 ((Activity) getContext()).finish(); 17 } 18 19 }); 20 titleEdit.setOnClickListener(new OnClickListener(){ 21 22 @Override 23 public void onClick(View v) { 24 25 // TODO Auto-generated method stub 26 Toast.makeText(getContext(), "You Click Edit button",Toast.LENGTH_SHORT).show(); 27 } 28 29 }); 30 } 31 }
這樣的話,每當們在一個布局中引入TitleLayout,返回按鈕和編輯按鈕的點擊事件就已經自動實現好了,也是省去了很多編寫重復代碼額工作。
自定義控件