Android 自定义View并添加属性(转)

http://blog.sina.com.cn/s/blog_5a6f39cf0101cfin.html

转自: http://www.cnblogs.com/trinea/archive/2012/11/14/2768271.html

本文主要介绍如何为自定义的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);
    }
    
    
    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值,如下
<manifestxmlns: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"


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值