自定义组合控件

来源:互联网 发布:空间数据的编辑 编辑:程序博客网 时间:2024/06/07 09:43

自定义组合控件

public class SettingItemView extends RelativeLayout {    //命名空间namespace    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);// 设置标题    }    //对外提供的在代码中动态设置控件的title    public void setTitle(String title) {        tvTitle.setText(title);    }    //对外提供的在代码中动态设置控件的desc    public void setDesc(String desc) {        tvDesc.setText(desc);    }    /**     * 返回勾选状态     *      * @return     */    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>

自定义属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="SettingItemView">        <attr name="title" format="string" />        <attr name="desc_on" format="string" />        <attr name="desc_off" format="string" />    </declare-styleable></resources>
0 0
原创粉丝点击