Android中的属性,是怎么应用到控件上的(三)

首先看一下最常用的方式

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值