Android declare-styleable自定义属性

  我们自己定义view,通常继承View,重写构造方法和onDraw等函数,然后具体实现自己定义的复杂view。但是继承这个自定义view的有的属性又各有不同,有的属性在原生属性中没有,这时候就可以使用自定义的属性来便捷的设置相应的属性。

1、在res/values/下新建一个attrs.xml文件。写入<resources><declare-styleable><attr/>这三级

标签。并填入相应属性。

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="myStyle">
        <attr name="textSize" format="dimension" />
        <attr name="backgroud" format="reference" />
        <attr name="text" format="string|reference" />
        <attr name="textColor" format="color" />
        <attr name="yesOrNot" format="boolean" />
        <attr name="number">
            <enum name="one" value="0" />
            <enum name="two" value="1" />
            <enum name="leftToRight" value="2" />
            <enum name="topToBottom" value="3" />
        </attr>
    </declare-styleable>

</resources></span>


2、建立一个自定义视图类,本次偷懒直接继承自textview。然后使用typedarray类来获取控件相应的

属性并使用。


<span style="font-size:18px;">import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyView extends TextView
{

    
    public MyView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.myStyle);
        
        float textSize = typedArray.getDimension(R.styleable.myStyle_textSize, 15);
        Drawable back = typedArray.getDrawable(R.styleable.myStyle_backgroud);
        String textString = typedArray.getString(R.styleable.myStyle_text);
        int textColor = typedArray.getInt(R.styleable.myStyle_textColor, Color.BLACK);
        boolean yesOrNot = typedArray.getBoolean(R.styleable.myStyle_yesOrNot, true);
        int number = typedArray.getInt(R.styleable.myStyle_number, 0);
        
        setTextSize(textSize);
        setText(textString);
        setBackgroundDrawable(back);
        setTextColor(textColor);
        
    }

}
</span>


3、在布局文件里引用该类,并设定相应的属性。记得要定义xmlns:myapp(后面的名称随意取,但要上下一致。)= "http://schemas.android.com/apk/res/com.example.declarestyleabletest"(最后是该自定义视图类所在的包名)


<span style="font-size:18px;"><span style="font-size:18px;"><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:myapp="http://schemas.android.com/apk/res/com.example.declarestyleabletest"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.declarestyleabletest.MainActivity"
    tools:ignore="MergeRootFrame" >

    <com.example.declarestyleabletest.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        myapp:backgroud="@drawable/ic_launcher"
        myapp:number="one"
        myapp:text="sdfkks"
        myapp:textColor="@color/black"
        myapp:textSize="15sp" />

</LinearLayout></span></span>

补充一些常用的format


reference  指定某资源ID
string   字符串
dimension   尺寸
color   颜色
boolean   布尔
integer   整型
float   浮点型
fraction   百分数
enum   枚举
flag  位运算


xml 中定义好自己的属性值,在在代码中得到值,或使用默认值

public CText(Context context, AttributeSet set) {
        super(context, set);
        this.context  = context;
        initView();
        TypedArray array = context.obtainStyledAttributes(set,R.styleable.CEditText);
        initAttrs(array);
    }

    private void initAttrs(TypedArray a) {
        hint = a.getString(R.styleable.CEditText_cHint);
        text = a.getString(R.styleable.CEditText_cText);
        setAttrs();
        a.recycle();
    }



转载请注明出处:http://blog.csdn.net/chenlinfeng772885775/article/details/50911702


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值