1. 程式人生 > >android之自定義組合控制元件

android之自定義組合控制元件

---------------setting_item---------------

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="80dp">
    <TextView
        android:layout_marginTop="10dp"
        android:id="@+id/tv_settting_item_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="22sp"
        android:text="設定是否自動更新"/>

    <TextView
        android:id="@+id/tv_setting_item_subtitle"
        android:layout_below="@+id/tv_settting_item_title"
        android:layout_marginTop="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="子標題"
        android:textSize="18sp"/>
    //下面的focusable和clickable是chekbox天生就有的屬性,點選的優先順序比組合控制元件高,所以吧組合控制元件設定了點選事件,就藥吧checkbox的點選事件去掉
    <CheckBox
        android:focusable="false"
        android:clickable="false"
        android:id="@+id/ck_setting_item"
        android:layout_marginRight="10dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
<View
    android:id="@+id/tv_setting_item_line"
    android:layout_alignParentBottom="true"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="#000000"
    android:layout_marginRight="1dp"/>
</RelativeLayout>
/**
 * Created by lambo on 2018/4/15.
 * 自定義組合控制元件---多個控制元件組成,可以複用
 */

public class Setting_item extends RelativeLayout {
CheckBox cb;
    private TextView tv_subtile;
    private TextView tv_title;
    private String descoff;
    private String descon;
    private String title;

    //帶一個引數的構造方法,初始化佈局檔案的時候呼叫
    public Setting_item(Context context) {
        super(context);
        initView(context);
    }

    private void initView(Context context) {
        //吧佈局檔案轉換成view
        View.inflate(context, R.layout.setting_item, this);
        cb=findViewById(R.id.ck_setting_item);
        tv_subtile = findViewById(R.id.tv_setting_item_subtitle);
        tv_title = findViewById(R.id.tv_settting_item_title);
    }
/*帶兩個引數的構造方法,佈局檔案使用的時候呼叫*/
    public Setting_item(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        //獲取名稱空間(http://schemas.android.com/apk/res-auto)下的屬性title,descon,descoff,在attr.xml中給textview中擴充套件了屬性
        title = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","title");
        descon = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","descon");
        descoff = attrs.getAttributeValue("http://schemas.android.com/apk/res-auto","descoff");
        tv_title.setText(title);
    }

    public Setting_item(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }
//檢驗組合控制元件是否選中
    public Boolean isChecked(){

        return cb.isChecked();
    }

    //設定組合控制元件
    public void setChecked(Boolean isChecked){
        if(isChecked){
            setSubtile(descon);
        }else {
            setSubtile(descoff);
        }
        cb.setChecked(isChecked);
    }

    //設定組合控制元件自標題的方法
    public void setSubtile(String  subtilte){

tv_subtile.setText(subtilte);
    }
}

---------------------res/values/attr.xml-----------------新增屬性

<?xml version="1.0" encoding="utf-8"?>

<resources>
    //給自定義的組合控制元件Setting_item擴充套件屬性
    <declare-styleable name="Setting_item">
        <attr name="title" format="string"/>
        <attr name="descoff" format="string"/>
        <attr name="descon" format="string"/>
    </declare-styleable>


</resources>