利用組合控件自定義Android控件

組合控件的用法:

第一步:新建一個佈局文件,在佈局文件中放置好相應的控件,代碼如下所示。
代碼出處:http://blog.csdn.net/guolin_blog/article/details/17357967


[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="50dp"  
  5.     android:background="#ffcb05" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button_left"  
  9.         android:layout_width="60dp"  
  10.         android:layout_height="40dp"  
  11.         android:layout_centerVertical="true"  
  12.         android:layout_marginLeft="5dp"  
  13.         android:background="@drawable/back_button"  
  14.         android:text="Back"  
  15.         android:textColor="#fff" />  
  16.   
  17.     <TextView  
  18.         android:id="@+id/title_text"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:layout_centerInParent="true"  
  22.         android:text="This is Title"  

第二步:創建一個類繼承自FrameLayout,在類的構造函數中用了LayoutInflater的inflate()方法來加載剛剛定義的xml佈局文件。關於LayoutInfalter的原理分析可以參考:Android LayoutInflater原理分析,帶你一步步深入瞭解View(一) 

參考代碼:

代碼出處:http://blog.csdn.net/guolin_blog/article/details/17357967

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. public class TitleView extends FrameLayout {  
  2.   
  3.     private Button leftButton;  
  4.   
  5.     private TextView titleText;  
  6.   
  7.     public TitleView(Context context, AttributeSet attrs) {  
  8.         super(context, attrs);  
  9.         LayoutInflater.from(context).inflate(R.layout.title, this);  
  10.         titleText = (TextView) findViewById(R.id.title_text);  
  11.         leftButton = (Button) findViewById(R.id.button_left);  
  12.         leftButton.setOnClickListener(new OnClickListener() {  
  13.             @Override  
  14.             public void onClick(View v) {  
  15.                 ((Activity) getContext()).finish();  
  16.             }  
  17.         });  
  18.     }  
  19.   
  20.     public void setTitleText(String text) {  
  21.         titleText.setText(text);  
  22.     }  
  23.   
  24.     public void setLeftButtonText(String text) {  
  25.         leftButton.setText(text);  
  26.     }  
  27.   
  28.     public void setLeftButtonListener(OnClickListener l) {  
  29.         leftButton.setOnClickListener(l);  
  30.     }  
  31.   
  32. }  

第三步:在佈局文件中引用自定義的View文件


代碼出處:http://blog.csdn.net/guolin_blog/article/details/17357967

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent" >  
  5.   
  6.     <com.example.customview.TitleView  
  7.         android:id="@+id/title_view"  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" >  
  10.     </com.example.customview.TitleView>  
  11.   
  12. </RelativeLayout>  

這樣就成功將一個標題欄控件引入到佈局文件中了,運行一下程序,效果如下圖所示:



發佈了49 篇原創文章 · 獲贊 21 · 訪問量 81萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章