自定义组合控件

一、步骤:
  1. 根据相应的布局文件类型来自定义一个类( SettingItemView  )继承与对应的布局类型的类(如: RelativeLayout  )
  2. 自定义空间的结构(或样式)

二、自定义控件布局样式例子:
  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="60dip" >
  5. <TextView
  6. android:layout_marginTop="5dip"
  7. android:layout_marginLeft="10dip"
  8. android:id="@+id/tv_title"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="设置自动更新"
  12. android:textSize="20sp" />
  13. <TextView
  14. android:layout_marginTop="5dip"
  15. android:layout_marginLeft="10dip"
  16. android:id="@+id/tv_desc"
  17. android:textColor="#55000000"
  18. android:layout_below="@id/tv_title"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="自动更新已经开启"
  22. android:textSize="16sp"
  23. />
  24. <CheckBox
  25. android:clickable="false"
  26. android:focusable="false"
  27. android:focusableInTouchMode="false"
  28. android:id="@+id/cb_isupdate"
  29. android:layout_width="wrap_content"
  30. android:layout_height="wrap_content"
  31. android:layout_centerVertical="true"
  32. android:layout_alignParentRight="true" />
  33. <View
  34. android:layout_width="match_parent"
  35. android:layout_height="0.2dip"
  36. android:background="#55000000"
  37. android:layout_alignParentBottom="true"
  38. />
  39. </RelativeLayout>

三、自定义一个类 (SettingItemView)

      该类所要继承的类是根据该布局文件的布局文件类型来定的,例如自定义的控件所用的布局为相对布局,该类就继承与RelativeLayout


  1. package com.app.mobilesafe.ui;
  2. import android.content.Context;
  3. import android.util.AttributeSet;
  4. import android.view.View;
  5. import android.widget.CheckBox;
  6. import android.widget.RelativeLayout;
  7. import android.widget.TextView;
  8. import com.itheima.mobilesafe.R;
  9. public class SettingItemView extends RelativeLayout{
  10. private CheckBox cb_isupdate;
  11. private TextView tv_desc;
  12. private TextView tv_title;
  13. private View view;
  14. private String update_off;
  15. private String update_on;
  16. private String title;
  17. /**
  18. * 在布局文件中使用这个控件时,如果指定了样式,系统初始化布局文件中的这个控件时,调用这个方法
  19. * @param context
  20. */
  21. public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
  22. super(context, attrs, defStyle);
  23. initView(context);
  24. }
  25. /**
  26. * 初始化布局文件中的这个控件时,系统默认调用这个方法 (该构造方法中有一个参数attrs,安卓底层利用反射将xml文件中的属性存放在AttributeSet 对象中)
  27. * @param context
  28. */
  29. public SettingItemView(Context context, AttributeSet attrs) {
  30. super(context, attrs);
  31. initView(context);
  32. //如果属性是是系统默认的namespace就写为null;  
  33.               String id=attrs.getAttributeValue(null,"id")+"";  
  34.              //如果属性是自定义的namespace就为写在相应xml文件中namespace
  35. update_off = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_off")+"";
  36. update_on = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "update_on")+"";
  37. title = attrs.getAttributeValue("http://schemas.android.com/apk/com.itheima.mobilesafe", "title")+"";
  38. tv_title.setText(title);
  39. }
  40. /**
  41. * 在代码new这个对象时使用这个方法
  42. * @param context
  43. */
  44. public SettingItemView(Context context) {
  45. super(context);
  46. initView(context);
  47. // TODO Auto-generated constructor stub
  48. }
  49. public void initView(Context context){
  50. view = View.inflate(context, R.layout.settings_item_view, this);
  51. tv_title = (TextView) view.findViewById(R.id.tv_title);
  52. tv_desc = (TextView) view.findViewById(R.id.tv_desc);
  53. cb_isupdate = (CheckBox) view.findViewById(R.id.cb_isupdate);
  54. }
  55. public boolean isChecked(){
  56. return cb_isupdate.isChecked();
  57. }
  58. public void setChecked(boolean checked) {
  59. if(checked){
  60. tv_desc.setText(update_on);
  61. }else{
  62. tv_desc.setText(update_off);
  63. }
  64. cb_isupdate.setChecked(checked);
  65. }
  66. }


四、在values文件夹下创建一个attrs.xml文件

        将要自定义的属性写在该xml文件中(注:谷歌官方推荐自定义的属性要在这里声明一下,但是在实际中不声明而是在该组合控件的java文件中声明也可以使用)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="SettingItemView"> 注:这里写的是该组合控件的类的名字
  4. <attr name="update_off" format="reference" />
  5. <attr name="update_on" format="reference"/>
  6. <attr name="title" format="reference"/>
  7. </declare-styleable>
  8. </resources>

五、自定义属性

        需要在SettingItemView.java的初始化时声明

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:safe="http://schemas.android.com/apk/com.app.safe"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical"
  7. >
  8. <TextView
  9. android:layout_width="match_parent"
  10. android:layout_height="40dp"
  11. android:gravity="center"
  12. android:text="设置中心"
  13. android:textSize="20sp"
  14. android:background="#ff0000"
  15. />
  16. <com.app.ui.SettingItemView
  17. android:id="@+id/my_setting"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content"
  20. safe:title="设置自动更新"
  21. safe:update_off="自动更新已经关闭"
  22. safe:update_on="自动更新已经开启"
  23. />
  24. <com.app.ui.SettingItemView
  25. android:id="@+id/app_lock_setting"
  26. android:layout_width="match_parent"
  27. android:layout_height="wrap_content"
  28. safe:title="设置程序锁"
  29. safe:update_on="程序锁已经开启"
  30. safe:update_off="程序锁已经关闭"
  31. />
  32. </LinearLayout>







posted @ 2015-03-12 00:05  就不呵呵呵  阅读(883)  评论(0编辑  收藏  举报