【Android】创建自定义复合控件

来源:互联网 发布:atheros ar9485 linux 编辑:程序博客网 时间:2024/05/05 16:55

复合控件是将一组相互关联的已有控件整合,从而可以当作单个控件使用。

 

创建复合控件的步骤:

 

 

  1. 创建一个扩展布局的类
  2. 实现构造方法,并在构造方法中,首先实现超类的构造方法super(...)
  3. 复合组件可以像其他视图一样在XML中声明创建,组件名为该类的完整名称(包名+类名),并在构造方法中实现超类构造方法super(Context contex, AttributeSet attrs)。
  4. 通过attrs获取自定义属性值,对子控件进行初始化设置。
  5. 根据需要扩展该复合组件方法
示例代码:
图标控件(包含图标和标题)
Java代码  收藏代码
  1. package lizhen.appstore.ext;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.drawable.Drawable;  
  5. import android.util.AttributeSet;  
  6. import android.view.Gravity;  
  7. import android.view.View;  
  8. import android.widget.ImageButton;  
  9. import android.widget.LinearLayout;  
  10. import android.widget.TextView;  
  11.   
  12. /** 
  13.  * 图标(图标+标题) 
  14.  * */  
  15. public class Icon extends LinearLayout {  
  16.       
  17.     private ImageButton icon; //图标  
  18.     private TextView title; //标题  
  19.       
  20.     public Icon(Context context, AttributeSet attrs) {  
  21.         super(context, attrs);  
  22.           
  23.         setOrientation(VERTICAL); //设置方向竖直  
  24.         /* 
  25.          * 图标初始化 
  26.          * */  
  27.         icon = new ImageButton(context);  
  28.         int srcID = attrs.getAttributeResourceValue(null"src"0);  
  29.         if(srcID == 0) {  
  30.             //TODO  
  31.         } else {  
  32.             icon.setImageResource(srcID);  
  33.         }  
  34.         LayoutParams layoutParames = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);  
  35.         icon.setPadding(5555);  
  36.         addView(icon, layoutParames);  
  37.         /* 
  38.          * 标题初始化 
  39.          * */  
  40.         title = new TextView(context);  
  41.         int textID = attrs.getAttributeResourceValue(null"text"0);  
  42.         if(textID == 0) {  
  43.             //TODO  
  44.         } else {  
  45.             title.setText(textID);  
  46.         }  
  47.         title.setGravity(Gravity.CENTER_HORIZONTAL);  
  48.         addView(title, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));  
  49.           
  50.     }  
  51.       
  52.     /** 
  53.      * 设置图标图像 
  54.      * resid 资源ID 
  55.      * */  
  56.     public void setImageResource(int resid) {  
  57.         icon.setImageResource(resid);  
  58.     }  
  59.       
  60.     /** 
  61.      * 设置图标图像 
  62.      * drawable Drawable图像 
  63.      * */  
  64.     public void setImageResource(Drawable drawable) {  
  65.         icon.setImageDrawable(drawable);  
  66.     }  
  67.       
  68.     /** 
  69.      * 设置图标标题 
  70.      * resid 资源ID 
  71.      * */  
  72.     public void setText(int resid) {  
  73.         title.setText(resid);  
  74.     }  
  75.       
  76.     /** 
  77.      * 设置图标标题 
  78.      * text 文本 
  79.      * */  
  80.     public void setText(CharSequence text) {  
  81.         title.setText(text);  
  82.     }  
  83.       
  84.     /** 
  85.      * 设置图标点击事件监听器 
  86.      * listener 点击事件监听器 
  87.      * */  
  88.     public void setOnIconClickListner(View.OnClickListener listener) {  
  89.         icon.setOnClickListener(listener);  
  90.     }  
  91.   
  92. }  
 
布局文件
Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"  
  6.     android:layout_height="fill_parent">  
  7.     <lizhen.appstore.ext.Icon  
  8.         android:id="@+id/icon"  
  9.         src="@drawable/icon"  
  10.         text="@strings/android"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"/>  
  13. </LinearLayout>  
0 0
原创粉丝点击