Android中的属性,是怎么应用到控件上的(三)
来源:互联网 发布:js 折叠 展开 编辑:程序博客网 时间:2024/05/16 08:49
首先看一下最常用的方式
1)源码中在Application中使用dialog的Style直接整个应用都应用上了,学源码的方式自定义Style看看。在TextView的构造函数中发现
public TextView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, com.android.internal.R.attr.textViewStyle); }就这个两个参数构造的它调用三个参数的构造,并且默默的加上了参数 com.android.internal.R.attr.textViewStyle ,源码中它是这样的
<!-- Default TextView style. --> <attr name="textViewStyle" format="reference" />
因此当使用源码TextView时,使用时候只要在values->styles.xml 主风格加上之后就可以了
<resources> <!-- Base application theme. --> <style name="AppTheme"> <item name="android:textViewStyle">@style/MyTextViewStyle</item> </style> <style name="MyTextViewStyle"> <item name="android:text">HELLO WORLD</item> <item name="android:textColor">@android:color/holo_red_light</item> <item name="android:textSize">100dp</item> <item name="android:background">@mipmap/ic_launcher</item> </style></resources>
显示效果如下:
所有的TextView都应用到了这个属性,包括左上角的那个TItlebar里的TextView,但是只用了一个背景,其他的属性没有应用到上面,
2)然后自己定义Style来使用
final TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.View, defStyleAttr, defStyleRes);
关键还是这个函数调用,这回终于用到了这第三个参数,第三个参数是android在Styles中解析的名称,在style中解析第二个参数中的属性(前提是第二个属性没有解析到的剩余属性),那么它的作用就是个后补的作用,在layout中可以解析到的,在style中就不用了,这就解释了优先级的问题。
第四个参数 defStyleRes 只有当第三个参数defStyleAttr 为0是才会使用,就是直接去Style资源里面找了。定义个style,跟第三个defStyleAttr 情形差不多,也是后补选手。
源码中大概就这样了,下面自定义View自定义Style
第一步:在attrs.xml 加上style属性名字
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="MyCustomStyle" format="reference" /> <declare-styleable name="MyCustomAttrs"> <attr name="mytextcolor" format="color"/> <attr name="mybackground" format="reference"/> <attr name="mytextsize" format="dimension"/> <attr name="mytext" format="string"/> </declare-styleable></resources>
比前一篇多了一句 ,这一句用来说明这是Style的名字
<attr name="MyCustomStyle" format="reference" />第二步:自定义View,将第三个参数填上R.attr.MyCustomStyle。
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomAttrs, R.attr.MyCustomStyle, defStyleRes);第三步:在styles.xml中定义个customStyle,这其中有自定义的属性 ,并应用到主题中应用到主Theme中
<?xml version="1.0" encoding="utf-8"?><resources> <!-- Base application theme. --> <style name="AppTheme"> <item name="MyCustomStyle">@style/customStyle</item> </style> <style name="customStyle"> <item name="mytext">HELLO WORLD</item> <item name="mytextcolor">@android:color/holo_red_light</item> <item name="mytextsize">50dp</item> <item name="mybackground">@mipmap/ic_launcher</item> </style></resources>
运行效果如下:
下面验证以下第四个参数 defStyleRes
final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyCustomAttrs, 0, R.style.customStyle);
在自定义View里修改成这个样子,运行结果如下
第四位后补这样才发挥它的作用
demo 地址 http://download.csdn.net/detail/qinxue24/9855453
- Android中的属性,是怎么应用到控件上的(三)
- Android中的属性,是怎么应用到控件上的(一)
- Android中的属性,是怎么应用到控件上的(二)
- Android中的属性,是怎么应用到控件上的(四)
- w3c系列之CSS(三):CSS属性是如何被应用到元素上的
- j2ee的web项目,中文怎么被保存到数据库中的(其实最终是保存在计算机上的)
- 搜狗云输入法是怎么飘到网页页面上的?
- Android 控件属性详解(三)
- 菜鸟是怎么练成的(三)
- platform_driver 中的 .remove 是怎么被调用到的
- Android的EditText控件点击虚拟键盘上的回车怎么跳到下一个EditText继续输入呢?
- 同事问到C#中的属性(Property)怎么理解?
- React Native是怎么在Android上跑起来的
- Android中的动画(三)属性动画
- 鼠标在slide控件的任意位置上点击,滑块滑动到该位置是如何实现的,有什么属性吗?
- android 在应用的配置文件设置apk的安装路径,以下是安装到sdcard上的方式
- Android应用view组件tablelayout应用(tablerow中的属性的应用)
- Android: /cache中的文件是怎么消失的
- 关于Java中反射的认识
- 【模板】ISAP网络最大流 (模板题:洛谷P3376)
- 1061. 判断题
- 1056. 组合数的和
- 【算法题】按单词逆序
- Android中的属性,是怎么应用到控件上的(三)
- poj 1611 The Suspects
- 1003. 我要通过!
- poj3744 概率dp + 矩阵快速幂
- 使用MySQL Workbench远程连接Ubuntu MySQL
- Windows下GitHub的使用
- JAVA 复习总结(三)
- 深度神经网络结构以及Pre-Training的理解
- Ruby中include / extend / prepend的区别