Android自定義組合控件

組合控件佈局文件

首先定義一個組合控件的佈局文件
view_setting_item.xml

<?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="70dp"
    android:padding="5dp" >

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/black"
        android:textSize="22sp" />

    <TextView
        android:id="@+id/tv_desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:layout_marginTop="3dp"
        android:textColor="#a000"
        android:textSize="18sp" />

    <CheckBox
        android:id="@+id/cb_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false" />

    <View
        android:layout_width="match_parent"
        android:layout_height="0.2dp"
        android:layout_alignParentBottom="true"
        android:background="#a000" />

</RelativeLayout>

定義一個包含組合控件的類

這個類可以是繼承至ViewGroup的任何類,這樣就可以將上面定義的佈局文件放在這個類中。
SettingItemView.java

package com.itheima52.mobilesafe.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.itheima52.mobilesafe.R;

/**
 * 設置中心的自定義組合控件
 */
public class SettingItemView extends RelativeLayout {
    // 定義一個命名空間
    private static final String NAMESPACE = "http://schemas.android.com/apk/res/com.itheima52.mobilesafe";
    private TextView tvTitle;
    private TextView tvDesc;
    private CheckBox cbStatus;
    private String mTitle;
    private String mDescOn;
    private String mDescOff;

    public SettingItemView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public SettingItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // 根據屬性名稱,獲取屬性的值
        mTitle = attrs.getAttributeValue(NAMESPACE, "title");
        mDescOn = attrs.getAttributeValue(NAMESPACE, "desc_on");
        mDescOff = attrs.getAttributeValue(NAMESPACE, "desc_off");
        initView();
        // 打印出所有的屬性
        // int attributeCount = attrs.getAttributeCount();
        //
        // for (int i = 0; i < attributeCount; i++) {
        // String attributeName = attrs.getAttributeName(i);
        // String attributeValue = attrs.getAttributeValue(i);
        //
        // System.out.println(attributeName + "=" + attributeValue);
        // }
    }

    public SettingItemView(Context context) {
        super(context);
        initView();
    }

    /**
     * 初始化佈局
     */
    private void initView() {
        // 將自定義好的佈局文件設置給當前的SettingItemView
        View.inflate(getContext(), R.layout.view_setting_item, this);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        tvDesc = (TextView) findViewById(R.id.tv_desc);
        cbStatus = (CheckBox) findViewById(R.id.cb_status);
        // 設置標題
        setTitle(mTitle);
    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setDesc(String desc) {
        tvDesc.setText(desc);
    }

    public boolean isChecked() {
        return cbStatus.isChecked();
    }

    public void setChecked(boolean check) {
        cbStatus.setChecked(check);
        // 根據選擇的狀態,更新文本描述
        if (check) {
            setDesc(mDescOn);
        } else {
            setDesc(mDescOff);
        }
    }
}

使用自定義組合控件

在佈局文件中使用我們自定義的組合控件,注意需要引入對應的命名空間。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:itheima="http://schemas.android.com/apk/res/com.itheima52.mobilesafe"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        style="@style/TitleStyle"
        android:text="設置中心" />

    <com.itheima52.mobilesafe.view.SettingItemView
        android:id="@+id/siv_update"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        itheima:desc_off="自動更新已關閉"
        itheima:desc_on="自動更新已開啓"
        itheima:title="自動更新設置" />

</LinearLayout>

訪問自定義的組合控件

通過id引用到我們的自定義組件過後,就可以調用這個view的各種方法來設置組合控件裏面各個控件的屬性了。

package com.itheima52.mobilesafe.activity;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

import com.itheima52.mobilesafe.R;
import com.itheima52.mobilesafe.view.SettingItemView;

/**
 * 設置中心
 * 
 */
public class SettingActivity extends Activity {

    private SettingItemView sivUpdate;// 設置升級
    private SharedPreferences mPref;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        mPref = getSharedPreferences("config", MODE_PRIVATE);
        sivUpdate = (SettingItemView) findViewById(R.id.siv_update);
        boolean autoUpdate = mPref.getBoolean("auto_update", true);
        if (autoUpdate) {
            sivUpdate.setChecked(true);
        } else {
            sivUpdate.setChecked(false);
        }

        sivUpdate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 判斷當前的勾選狀態
                if (sivUpdate.isChecked()) {
                    // 設置不勾選
                    sivUpdate.setChecked(false);
                    // 更新sp
                    mPref.edit().putBoolean("auto_update", false).commit();
                } else {
                    sivUpdate.setChecked(true);
                    // 更新sp
                    mPref.edit().putBoolean("auto_update", true).commit();
                }
            }
        });
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章