Android UI进阶之自定义组合控件


今天和大家分享下组合控件的使用。很多时候Android自定义控件并不能满足需求,如何做呢?很多方法,可以自己绘制一个,可以通过继承基础控件来重写某些环节,当然也可以将控件组合成一个新控件,这也是最方便的一个方法。今天就来介绍下如何使用组合控件,将通过两个实例来介绍。

第一个实现一个带图片和文字的按钮,如图所示:


整个过程可以分四步走。第一步,定义一个layout,实现按钮内部的布局。代码如下:

[html]

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="horizontal"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <ImageView  
  8.     android:layout_width="wrap_content"  
  9.     android:layout_height="wrap_content"  
  10.     android:id="@+id/iv"  
  11.     android:src="@drawable/confirm"  
  12.     android:paddingTop="5dip"  
  13.     android:paddingBottom="5dip"  
  14.     android:paddingLeft="40dip"  
  15.     android:layout_gravity="center_vertical"  
  16.     />  
  17. <TextView  
  18.     android:layout_width="wrap_content"  
  19.     android:layout_height="wrap_content"  
  20.     android:text="确定"  
  21.     android:textColor="#000000"  
  22.     android:id="@+id/tv"  
  23.     android:layout_marginLeft="8dip"  
  24.     android:layout_gravity="center_vertical"  
  25.     />  
  26. </LinearLayout>  

  这个xml实现一个左图右字的布局,接下来写一个类继承LinearLayout,导入刚刚的布局,并且设置需要的方法,从而使的能在代码中控制这个自定义控件内容的显示。代码如下:

[java]

  1. package com.notice.ib;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.widget.ImageView;  
  7. import android.widget.LinearLayout;  
  8. import android.widget.TextView;  
  9.   
  10. public class ImageBt extends LinearLayout {  
  11.   
  12.     private ImageView iv;  
  13.     private TextView  tv;  
  14.   
  15.     public ImageBt(Context context) {  
  16.         this(context, null);  
  17.     }  
  18.   
  19.     public ImageBt(Context context, AttributeSet attrs) {  
  20.         super(context, attrs);  
  21.         // 导入布局   
  22.         LayoutInflater.from(context).inflate(R.layout.custombt, thistrue);  
  23.         iv = (ImageView) findViewById(R.id.iv);  
  24.         tv = (TextView) findViewById(R.id.tv);  
  25.   
  26.     }  
  27.   
  28.     /** 
  29.      * 设置图片资源 
  30.      */  
  31.     public void setImageResource(int resId) {  
  32.         iv.setImageResource(resId);  
  33.     }  
  34.   
  35.     /** 
  36.      * 设置显示的文字 
  37.      */  
  38.     public void setTextViewText(String text) {  
  39.         tv.setText(text);  
  40.     }  
  41.   
  42. }  
 第三步,在需要使用这个自定义控件的layout中加入这控件,只需要在xml中加入即可。方法如下:

[html]

  1. <RelativeLayout   
  2.          android:orientation="horizontal"  
  3.          android:layout_width="fill_parent"  
  4.          android:layout_height="wrap_content"  
  5.          android:layout_gravity="bottom"  
  6.          >  
  7.          <com.notice.ib.ImageBt  
  8.              android:id="@+id/bt_confirm"  
  9.              android:layout_height="wrap_content"  
  10.              android:layout_width="wrap_content"  
  11.              android:layout_alignParentBottom="true"  
  12.              android:background="@drawable/btbg"  
  13.              android:clickable="true"  
  14.              android:focusable="true"  
  15.              />  
  16.          <com.notice.ib.ImageBt  
  17.              android:id="@+id/bt_cancel"  
  18.              android:layout_toRightOf="@id/bt_confirm"  
  19.              android:layout_height="wrap_content"  
  20.              android:layout_width="wrap_content"  
  21.              android:layout_alignParentBottom="true"  
  22.              android:background="@drawable/btbg"  
  23.              android:clickable="true"  
  24.              android:focusable="true"  
  25.             />  
  26.          </RelativeLayout>  

注意的是,控件标签使用完整的类名即可。为了给按钮一个点击效果,你需要给他一个selector背景,这里就不说了。 

  • 1
  • 2
  • 下一页

相关内容