typearray和obtainStyledAttribute的作用
2015-12-04 10:32
585 查看
通常是在资源文件下创建attrs.xml
1.attrs.xml
2.布局文件如下:
3.在程序下获取该值
4.
1.attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="button"> <attr name="textSize" format="dimension" /> </declare-styleable> </resources> 根标签要是resources,定义的变量要有个名字,declare-styleable name="button">,这里定义名称为button。在这个名称里,可以有多个自定义属性。定义了个名为textSize的属性,格式是dimension,这个format指定了textSize属性的类型,只能用于定义字体大小。
2.布局文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:myapp="http://schemas.android.com/apk/res/com.easymorse.textbutton" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/background_color"> <LinearLayout android:layout_width="fill_parent" android:layout_height="10dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="40dip"> <com.easymorse.textbutton.TextButton android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:text="电影" android:gravity="center_vertical|center_horizontal" android:background="@drawable/button" android:focusable="true" android:clickable="true" myapp:textSize="20sp" /> 根标签中多了一个 xmlns:myapp myapp:textSize="20sp"
3.在程序下获取该值
public TextButton(final Context context, AttributeSet attrs) { this(context, attrs, 0); TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.button); this.setTextSize(typedArray.getDimension(R.styleable.button_textSize, 15)); typedArray.recycle(); TypedArray 和obtainStyledAttributes(AttributeSet attrs,R.styleable.button)
4.
这里的自定义属性的format,可以有很多种: reference string color dimension boolean integer float fraction enum flag
相关文章推荐
- iar for stm8 报错Error[Li005]: no definition for "assert_failed"解决方案
- iar for stm8 报错Error[Li005]: no definition for "assert_failed"解决方案
- linq中的contains条件
- Perl Debug error: SetConsoleMode failed, LastError=|6|
- HD1532Drainage Ditches(最大流模板裸题 + 邻接表)
- main.obj:-1: error: LNK2019: 无法解析的外部符号 "public: __thiscall CFindFileForm::CFindFileForm(class QWidge
- http://blog.csdn.net/wankunde/article/details/41675079/Spark 学习入门教程
- hdu3487 Play with Chain
- C++拷贝构造函数详解 http://blog.csdn.net/lwbeyond/article/details/6202256
- 崽崽帮www.zaizaibang.com精选14
- 崽崽帮www.zaizaibang.com精选3
- 崽崽帮www.zaizaibang.com精选2
- 崽崽帮www.zaizaibang.com精选1
- 特征提取http://blog.csdn.net/passball/article/details/5204132
- Debug Assertion Failed!
- 解决Gradle project sync failed
- SVN服务器端安装过程出现“Custom action InstallWMISchemaExecute failed:无法启动服务,原因可能是已被禁用或与其相关联的设备没有启动。”
- c++中类模板(class template)简单示例 http://blog.csdn.net/richerg85/article/details/7565870
- Daily Scrum - 12/03
- 百度 地图 slidingmenu 黑边 使用截图的方式解决黑边问题,步骤: 1.slidingMenu打开的时候调用BaiduMap的snapshot方法截图获取Bitmap对象; 2.使用