安卓自定义控件--TypedArray 详解
来源:互联网 发布:linux如何打开pdf文件 编辑:程序博客网 时间:2024/06/16 03:40
我本是在阅读TvRecyclerView的源码。我没有弄明白默认的选中图标到底如何设置的。我就去看源码,看到使用了TypeArray,于是记录下来,方便以后查看。
使用流程如下:
在attrs.xml中定义一个属性变量
<?xml version="1.0" encoding="utf-8" ?><resources> <declare-styleable name="TvRecyclerView"> <attr name="scrollMode"/> <attr name="focusDrawable" format="reference" /> <attr name="isAutoProcessFocus" format="boolean" /> <attr name="focusScale" format="float" /> </declare-styleable></resources>
在layout布局中
xmlns:app="http://schemas.android.com/apk/res-auto"
此句是声明自己的命名空间app:focusDrawable="@drawable/default_focus"
focusDrawable是自定义的属性<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:background="#000000"> <app.com.tvrecyclerview.TvRecyclerView android:id="@+id/tv_recycler_view" android:layout_width="match_parent" android:layout_height="580dp" android:layout_centerInParent="true" android:layout_margin="20dp" app:focusDrawable="@drawable/default_focus"/></RelativeLayout>
在自定义控件中要用到在布局文件中定义的属性值获取方式如下
public TvRecyclerView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); setAttributeSet(attrs); //省去一些代码}
private void setAttributeSet(AttributeSet attrs) { if (attrs != null) { TypedArray typeArray = getContext().obtainStyledAttributes(attrs, R.styleable.TvRecyclerView); int type = typeArray.getInteger(R.styleable.TvRecyclerView_scrollMode, 0); if (type == 1) { mIsFollowScroll = true; } final Drawable drawable = typeArray.getDrawable(R.styleable.TvRecyclerView_focusDrawable); if (drawable != null) { setFocusDrawable(drawable); } mSelectedScaleValue = typeArray.getFloat(R.styleable.TvRecyclerView_focusScale, DEFAULT_SELECT_SCALE); mIsAutoProcessFocus = typeArray.getBoolean(R.styleable.TvRecyclerView_isAutoProcessFocus, true); if (!mIsAutoProcessFocus) { mSelectedScaleValue = 1.0f; setChildrenDrawingOrderEnabled(true); } typeArray.recycle(); } if (mIsAutoProcessFocus) { // set TvRecyclerView process Focus setDescendantFocusability(ViewGroup.FOCUS_BEFORE_DESCENDANTS); }}
阅读全文
0 0
- 安卓自定义控件--TypedArray 详解
- Iwfu-安卓自定义控件xml属性---TypedArray
- 自定义view控件 TypedArray
- Android 自定义控件之第二讲:TypedArray 详解
- 自定义安卓控件
- 安卓----自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 安卓自定义控件
- 读书笔记之安卓控件架构与自定义详解
- 自定义控件属性(attr.xml,TypedArray)
- android自定义控件 ( TypedArray 、declare-styleable)
- 自定义控件的 TypedArray和obtainStyledAttributes使用
- 自定义控件5---TypedArray和AttributeSet
- Gradle使用/问题
- mamp pro无法打开mysql解决办法
- 字符串方法substring()的参数设置
- Ubuntu LXC
- asio 协程中 yield
- 安卓自定义控件--TypedArray 详解
- HashMap,Hashtable,SynchronizedHashMap,ConcurrentHashMap
- java 线程作业2
- webpack-stream
- 用sqoop导入数据到HIVE和HDFS中
- LintCode 第46题 主元素 【贪心算法】
- 菜鸟学MVC遇到的问题(一)
- <url-pattern>中的 *.action *.do /*
- fullcalendar 属性修改General Display