[android] 手机卫士自定义控件的属性

[android] 手机卫士自定义控件的属性,第1张

概述上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性 上一节组合控件SettingItemView中有三个控件,分别是TextView大标题,TextView描述,

上一节完成的自定义组合控件,灵活性不够,控件的显示信息上,仿照系统属性,自定义自己的属性

上一节组合控件SettingItemVIEw中有三个控件,分别是TextVIEw大标题,TextVIEw描述,CheckBox复选框

自定义属性 tsh:Title=”大标题” 和tsh:desc_on=”小标题开启”,tsh:desc_off=”小标题关闭”

添加命名空间,xmlns:tsh=”http://schemas.androID.com/apk/res/包名"

res/values/目录下创建 attrs.xml文件

添加节点 <declare-styleable name=”TextVIEw”>

节点下添加节点<attr name=”Title” format=”string”/>,添加其他两个属性的节点

 

在布局文件使用的时候,会调用带有两个参数的构造方法

在这个构造方法里面,会传递一个AttributeSet对象

调用AttributeSet对象的getAttributeValue()方法,得到属性值,参数:索引位置,不推荐

调用AttributeSet对象的getAttributeValue(namespace,name)方法,参数:命名空间,属性名

调用TextVIEw对象的setText()方法,直接给设置进去

描述部分,在setChecked()方法里面,判断,再设置

SettingItemVIEw.java

package com.qingguow.mobilesafe.ui;import androID.content.Context; androID.util.AttributeSet; androID.vIEw.VIEw; androID.Widget.CheckBox; androID.Widget.relativeLayout; androID.Widget.TextVIEw; com.qingguow.mobilesafe.R;public class SettingItemVIEw extends relativeLayout {    private TextVIEw tv_Title;     TextVIEw tv_desc;     CheckBox cb_status;     String desc_on;     String desc_off;    /**     * 初始化VIEw对象     * @param context     */    private voID initVIEw(Context context) {        VIEw.inflate(context,R.layout.setting_item_vIEw,this);        cb_status=(CheckBox) .findVIEwByID(R.ID.cb_status);        tv_desc=(TextVIEw) .findVIEwByID(R.ID.tv_desc);        tv_Title=(TextVIEw) .findVIEwByID(R.ID.tv_Title);            }         * 判断是否选中     * @return     boolean isChecked(){        return cb_status.isChecked();    }         * 设置是否选中     *  status     voID setChecked( status){        if(status){            tv_desc.setText(desc_on);        }else{            tv_desc.setText(desc_off);        }        cb_status.setChecked(status);    }         * 设置显示文本     *  text      setDesc(String text){        tv_desc.setText(text);    }    public SettingItemVIEw(Context context,AttributeSet attrs,int defStyle) {        super(context,attrs,defStyle);        initVIEw(context);    }    public SettingItemVIEw(Context context,AttributeSet attrs) {        );        tv_Title.setText(Title);        desc_on=attrs.getAttributeValue("http://schemas.androID.com/apk/res/com.qingguow.mobilesafe","desc_on");        desc_off=attrs.getAttributeValue("http://schemas.androID.com/apk/res/com.qingguow.mobilesafe","desc_off");    }     SettingItemVIEw(Context context) {        (context);        initVIEw(context);    }}

 

activity_setting.xml

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:tsh="http://schemas.androID.com/apk/res/com.qingguow.mobilesafe"    androID:layout_wIDth="match_parent"    androID:layout_height    androID:orIEntation="vertical" >    TextVIEw        androID:layout_wIDth        androID:layout_height="40dp"        androID:background="#ccc"        androID:gravity="center"        androID:text="设置中心"        androID:textSize="20sp" />    com.qingguow.mobilesafe.ui.SettingItemVIEw        tsh:Title="设置自动更新"        tsh:desc_on="设置自动更新开启"        tsh:desc_off="设置自动更新关闭"        androID:layout_wIDth="wrap_content"        androID:ID="@+ID/siv_item">    </com.qingguow.mobilesafe.ui.SettingItemVIEw>linearLayout>

attrs.xml

resourcesdeclare-styleable name="TextVIEw">        attr ="Title" format="string" />        ="desc_on"="desc_off"/>    declare-styleable>

 

总结

以上是内存溢出为你收集整理的[android] 手机卫士自定义控件的属性全部内容,希望文章能够帮你解决[android] 手机卫士自定义控件的属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1121149.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存