Android中attr详解
2015-09-24 20:00
417 查看
在学习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获得的一系列的基本的属性值,不需要用用一个主题或者/和样式资源执行样式。 参数: set:现在检索的属性值; attrs:制定的检索的属性值
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 冒号后面是标签名。 com.iteye.googlers 是你的工程包名。 使用方式: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); } }
相关文章推荐
- Android之手势的识别与处理(双击onDoubleTap、滑动onFling、拖动onScroll)
- Android中的Adapter 详解
- Android事件传递机制、手势检测
- NDK使用步骤
- Android--Gradle的理解
- Android开发环境部署
- Android批量打包-如何一秒内打完几百个apk渠道包
- Android批量打包-如何一秒内打几十个apk渠道包
- Android 手势操作GestureDetector
- android内存文件读写
- Android.mk写法
- 线程池ThreadPoolExecutor使用简介
- android四大组件详解
- Android中自定义弹窗提醒控件
- Android学习实践:7.使用布局文件进行事件绑定
- 编写 Android 触摸屏手势识别程序
- android 的wifi设置
- Android的事件传递(仿下拉刷新)
- AndroidService 深度分析(2)
- Android 自定义View修炼-高仿猎豹清理大师自定义内存开口圆环比例进度View