• Android开发学习笔记-自定义控件的属性


    若想让自定义控件变得更加方便灵活,则就需要对控件进行定义属性,使其用起来更方便。

    下面是自定义控件属性的方法

    1、添加attrs.xml,内容格式样式可以参考sdkplatformsandroid-10data esvaluesattrs.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
         <declare-styleable name="CompoundButton">
         
            <attr name="desc_title" format="string" />
            <attr name="desc_on" format="string"/>
            <attr name="desc_off" format="string"/>
        </declare-styleable>
    </resources>

    2、在布局文件中引用命名空间,并设置自定义属性值,其中命名控件后面的为应用程序包名

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:frank="http://schemas.android.com/apk/res/com.frank.mobilesafe" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_maintitle"
            android:layout_width="match_parent"
            android:layout_height="55dp"
            android:background="#8866ff00"
            android:gravity="center"
            android:text="设置中心"
            android:textSize="22sp" />
    
       <com.frank.mobilesafe.ui.SettingItemView
           frank:desc_title="有新版本升级控制"
           frank:desc_on="有新版本则进行升级"
           frank:desc_off="不进行升级"
           android:id="@+id/siv_update"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           />
    </LinearLayout>

    3、在自定义控件的类里面的构造方法中对属性值进行获取或者设置。

    public SettingItemView(Context context, AttributeSet attrs) {
            super(context, attrs);
            initView(context);
            String myNamespace = "http://schemas.android.com/apk/res/com.frank.mobilesafe";
            str_desc_title = attrs.getAttributeValue(myNamespace,
                    "desc_title");
            str_desc_on = attrs.getAttributeValue(myNamespace,
                    "desc_on");
            str_desc_off = attrs.getAttributeValue(myNamespace,
                    "desc_off");
            tv_update_title.setText(str_desc_title);
    
        }

    4、可对自定义属性值做其他用途的处理

    /**
         * 设置组合控件的状态
         * 
         * @param isChecked
         */
        public void SetChecked(boolean isChecked) {
            cb_update.setChecked(isChecked);
            if(isChecked)
            {
                SetDesc(str_desc_on);
            }
            else
            {
                SetDesc(str_desc_off);
            }
        }
    
        /**
         * 设置描述信息
         * 
         * @param isChecked
         */
        public void SetDesc(String text) {
            tv_update_content.setText(text);
        }
  • 相关阅读:
    CSS 换行
    CSS div仿table样式
    CSS 选择器优先级
    ajax 请求前后处理
    JQuery 数组按指定长度分组
    JQuery .width()/.css("width")方法 比较
    JS 生成唯一值UUID
    JS/jQuery点击某元素之外触发事件
    JS/Jquery关系
    利用Nginxcp为cPanel/WHM服务器开启nginx支持
  • 原文地址:https://www.cnblogs.com/xuhongfei/p/4014570.html
Copyright © 2020-2023  润新知