• Android 自定义View并添加属性


    本文主要介绍如何为自定义的View添加属性以及属性的类型。代码示例定义见DropDownToRefreshListView,调用见DropDownToRefreshListViewDemo

    1、添加自定义View的属性文件
    在res/values中新建attrs.xml文件(文件名可另取,不过推荐用attrs.xml,可以将自定义属性都放入其中),内容为

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <declare-styleable name="myViewDefinedAttr"> 
    <attr name="attr1" format="boolean"/> 
    <attr name="attr2" format="integer"/>
    </declare-styleable> 
    </resources>

    定义名为myViewDefinedAttr的属性列表,这个name命名也可以用下划线形式。name会在下面第二步中使用。

    <attr name="attr1" format="boolean"/> 其中name为属性名,format为属性类型,可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。具体定义及调用方式见本文最后关于自定义属性的介绍。

    2、自定义View中获取属性值

    public class MyView extends View {
    
        private boolean attr1;
        private int     attr2;
        
        public MyView(Context context){
            super(context);
        }
        
        public MyView(Context context, AttributeSet attrs){
            super(context, attrs);
            getAttrs(context, attrs);
        }
    
        public MyView(Context context, AttributeSet attrs, int defStyle){
            super(context, attrs, defStyle);
            getAttrs(context, attrs);
        }
        
        /**
         * 得到属性值
         * 
         * @param context
         * @param attrs
         */
        private void getAttrs(Context context, AttributeSet attrs) {
            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.myViewDefinedAttr);
            attr1 = ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);
            attr2 = ta.getInt(R.styleable.myViewDefinedAttr_attr2, 0);
            ta.recycle();
        }
    }

    从上面可以看出,主要是通过context.obtainStyledAttributes得到属性及其值的对应列表。

    ta.getBoolean(R.styleable.myViewDefinedAttr_attr1, true);表示得到属性名为attr1的boolean值,若不存在该属性,则默认为true。这里的R.styleable.myViewDefinedAttr_attr1为第一步中的属性列表名_属性名。
    ta.recycle();为回收系统资源。


    3、调用自定义View

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <com.trinea.android.common.view.MyView
            android:id="@+id/app_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fastScrollEnabled="true"
            myViewXmlns:attr1="false"
            myViewXmlns:attr2="1"
            android:focusable="true" />
    
        ……
    </RelativeLayout>

    调用的主体为<com.trinea.android.common.view.MyView ……/>中内容,需要注意的是跟上面代码类似在外面的布局文件中加入自己的命名空间再通过命名空间调用属性,即

    xmlns:myViewXmlns="http://schemas.android.com/apk/res/com.trinea.mypackage",其中myViewXmlns为空间名可自取,值为http://schemas.android.com/apk/res/加当前应用的包名,即AndroidManifest.xml中manifest节点的package值,如下
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.trinea.mypackage"

    4、自定义属性的类型

    format表示的属性类型可以为boolean, string, integer, dimension, float, reference, color, fraction, enum, flag及其混合。
    (1) boolean表示布尔值,调用如 xx:attr1="false"
    (2) integer表示整型,调用如 xx:attr1="1"
    (3) dimension表示尺寸值,调用如 xx:attr1="42dp"
    (4) float表示浮点型,调用如 xx:attr1="0.7"
    (5) color表示颜色值,调用如 xx:attr1="#00FF00"
    (6) string表示字符串,调用如 xx:attr1="#adbddd"
    (7) reference表示参考某一资源id,调用如 xx:attr1 = "@drawable/图片ID"
    (8) fraction表示百分数,调用如 xx:attr1="30%"
    以上类型定义都为<attr name="attr1" format="xxxtype"/>

    (9) enum表示枚举值,定义为

    <attr name="enum_attr">
          <enum name="horizontal" value="0" />
          <enum name="vertical" value="1" />
    </attr>

    调用如 xx:attr1="horizontal"

    (10) flag表示位或运算,定义为

    <attr name="windowSoftInputMode">
        <flag name = "stateUnspecified" value = "0" />
        <flag name = "stateUnchanged" value = "1" />
        <flag name = "stateHidden" value = "2" />
        <flag name = "stateAlwaysHidden" value = "3" />
        <flag name = "stateVisible" value = "4" />
        <flag name = "stateAlwaysVisible" value = "5" />
        <flag name = "adjustUnspecified" value = "0x00" />
        <flag name = "adjustResize" value = "0x10" />
        <flag name = "adjustPan" value = "0x20" />
        <flag name = "adjustNothing" value = "0x30" />
     </attr> 

    调用如:xx:attr1="stateUnspecified | stateUnchanged | stateHidden"

    (11) 混合类型,定义为

    <declare-styleable name = "combine_type">
        <attr name = "background" format = "reference|color" />
    </declare-styleable>

    调用如 xx:attr1 = "@drawable/图片ID|#DDFF00"

  • 相关阅读:
    并查集
    树状数组及二维树状数组
    maven工程编译成jar包
    The Salt Master has rejected this minion's public key!
    salt-minion dead but pid file exists 正确解决方法
    mysql 查找表的auto_increment和修改
    fastjson --JSONObject 和JSONArray 转换
    git 获取当前版本的commitid
    fastjson 使用笔记
    spring IOC 注解@Resource
  • 原文地址:https://www.cnblogs.com/trinea/p/2768271.html
Copyright © 2020-2023  润新知