Android中attr详解

来源:互联网 发布:新淘宝账号可以开店吗 编辑:程序博客网 时间:2024/06/05 22:25

在学习attr用法之前先介绍TypedArray类的用法


1、TypedArray类

让我们先来看看源码中对TypedArray的解释:
这里写图片描述
大概意识是:包含函数 obtainStyledAttributes(AttributeSet, int[], int, int) 或者 obtainAttributes(AttributeSet, int[])检索的数组值。在执行完之后,一定要确保调用 recycle()函数。用于检索从这个结构对应于给定的属性位置到obtainStyledAttributes中的值。
实例(自定义attr.xml):

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="GifView">        <attr name="gif" format="reference" />        <attr name="paused" format="boolean" />    </declare-styleable></resources>  

在代码中的使用方式:

final TypedArray array = context.obtainStyledAttributes(attrs,                    R.styleable.MyGiftView, defStyle,         R.style.Widget_MyGiftView);mGiftId = array.getResourceId(R.styleable.MyGiftView_gif, -1);mPaused = array.getBoolean(R.styleable.MyGiftView_paused, false);array.recycle();

涉及的函数介绍:

obtainStyledAttributes(AttributeSet, int[], int, int)obtainAttributes(AttributeSet, int[])说明:返回一个由AttributeSet获得的一系列的基本的属性值,不需要用用一个主题或者/和样式资源执行样式。

2、为什么需要在TypedArray后调用recycle

当我们没有在使用TypedArray后调用recycle,编译器会提示“This TypedArray should be recycled after use with #recycle()“。官方的解释是:回收TypedArray,以便后面重用。在调用这个函数后,你就不能再使用这个TypedArray。在TypedArray后调用recycle主要是为了缓存。当recycle被调用后,这就说明这个对象从现在可以被重用了

attr使用

1、使用之前必须先在res/values 文件下定义一个attrs.xml 文件:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyGiftView">        <attr name="gif" format="reference" />        <attr name="paused" format="boolean" />    </declare-styleable>    <declare-styleable name="CustomTheme">        <attr name="gifViewStyle" format="reference" />    </declare-styleable></resources>  <!--    其中name表示自定义属性名,format表示自定义属性名对应的属性,其中reference表示:某一资源ID,boolean表示:布尔值。 -->

2、在布局文件中的使用方式

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    xmlns:gif="http://schemas.android.com/apk/res-auto"    android:gravity="center"    android:orientation="vertical" >    <com.practice.noyet.giftest.MyGiftView        android:id="@+id/gif1"        android:layout_width="150dp"        android:layout_height="150dp"        android:layout_gravity="center_horizontal"        android:enabled="false" />    <com.practice.noyet.giftest.MyGiftView        gif:gif="@raw/gift2"        android:id="@+id/gif2"        android:layout_width="150dp"        android:layout_height="150dp"        android:layout_gravity="center_horizontal"        android:enabled="false" /></LinearLayout><!--    其中 xmlns:gif="http://schemas.android.com/apk/res-auto" 是自定义标签。    xmlns:gif 冒号后面是标签名。    使用方式:gif:属性名 -->

3、在代码总的使用方式

    public MyGiftView(Context context) {        this(context, null);    }    public MyGiftView(Context context, AttributeSet attrs) {        this(context, attrs, R.styleable.CustomTheme_gifViewStyle);    }    public MyGiftView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        setViewAttributes(context, attrs, defStyleAttr);    }    /**     *     * @param context 上下文     * @param attrs 自定义属性     * @param defStyle 默认风格     */    @SuppressLint("NewApi")    private void setViewAttributes(Context context, AttributeSet attrs,                                   int defStyle) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {            setLayerType(View.LAYER_TYPE_SOFTWARE, null);        }        final TypedArray array = context.obtainStyledAttributes(attrs,                R.styleable.MyGiftView, defStyle, R.style.Widget_MyGiftView);        mGiftId = array.getResourceId(R.styleable.MyGiftView_gif, -1);        mPaused = array.getBoolean(R.styleable.MyGiftView_paused, false);        array.recycle();        if (mGiftId != -1) {            byte[] bytes = getGiftBytes();            mMovie = Movie.decodeByteArray(bytes, 0, bytes.length);        }    }
0 0