• android Theme使用三


    ☆ obtainStyledAttributes参数说明 和使用说明

    1)  obtainStyledAttributes(int[]attrs)

    int[] attrs返回的是attrs.xml里一个styleable包含的属性数组。

    <declare-styleablename="Tip">

            <attr name="left_icon"format="reference" />

            <attr name="tiptextSize"format="dimension" />

            <attr name="close_icon"format="reference" />

            <attr name="bgcolor"format="color" />

    </declare-styleable>

    这段styleable,使用obtainStyledAttributes(R.styleable.Tip);返回Tip包含的4个属性数组。

    这些属性的值我们写在哪儿呢?这个方法经过我的测试,这些属性的值必须写在application中android:theme对应的style下,什么意思?

    就是项目Manifest.xml文件中application节点下android:theme对应的值,即:

     

    <application

            android:name="com.My.module.App"

            android:allowBackup="true"

            android:icon="@drawable/logo"

           android:label="@string/app_name"

            android:theme="@style/My.Theme.dalancon">

    就是名称name=”My.Theme.dalancon”的style。

    <stylename="My.Theme.dalancon" parent="@style/My.Theme">

            <item name="vpiTabPageIndicatorStyle">@style/My.TabPageIndicator</item>

            <item name="left_icon">@drawable/comment_btn</item>

            <item name="tiptextSize">8sp</item>

            <item name="close_icon">@drawable/btn_delete</item>

            <item name="bgcolor">#FF5601</item>

    </style>

    这样程序代码才能够获取,如果你把这些属性值写在其他的style中,将没有效果。

    2)  obtainStyledAttributes(intresid, int[] attrs)

    int[] attrs : attrs返回的是attrs.xml里一个styleable包含的属性数组。

             int resid : 完成attrs数组中的属性赋值的Style资源name

     

    比如: a =context.obtainStyledAttributes(R.style.tipStyle, R.styleable.Tip);

    <stylename="tipStyle">

            <item name="left_icon">@drawable/comment_btn</item>

            <item name="tiptextSize">8sp</item>

            <item name="close_icon">@drawable/btn_delete</item>

            <item name="bgcolor">#FF5601</item>

    </style>

     

    和第一种obtainStyledAttributes(int[]attrs) 情况不同,这次的属性只要在一个独立的style下赋值就OK了。跟theme对应的style没关系。

     

    3)  obtainStyledAttributes(AttributeSetset, int[] attrs)

     

    相当于context.obtainStyledAttributes(AttributeSetset, int[],  0,  0);的情况

    4)  obtainStyledAttributes(AttributeSetset, int[] attrs, int defStyleAttr, int defStyleRes)

    set:可以传空null

    attrs : attrs返回的是attrs.xml里一个styleable包含的属性数组。

    defStyleAttr: 一个在attrs.xml中声明的属性名。R.attr.tipStyle

    defStyleRes:一个在styles.xml或themes.xml中声明的style的id。R.style.tipStyle

    说明一下最后两个参数:

    1.        defStyleAttr

    a)        当defStyleAttr等于attrs.xml中声明的一个属性

    <attrname="tipStyle" format="reference"></attr> 时候

    format是引用类型,表示我们要在theme中对tipStyle这个属性进行赋值。

    <stylename="tipStyle">

            <item name="left_icon">@drawable/comment_btn</item>

            <item name="tiptextSize">8sp</item>

            <item name="close_icon">@drawable/btn_delete</item>

            <item name="bgcolor">#FF5601</item>

    </style>

    然后在theme中使用它

    <stylename="My.Theme.dalancon" parent="@style/My.Theme">

       <item name="vpiTabPageIndicatorStyle">@style/My.Widget.TabPageIndicator</item>

       <item name="tipStyle">@style/tipStyle</item>

    </style>

     

    b)        当defStyleAttr等于0,表示没有默认值,

     

    2.        defStyleRes

     

    a)        defStyleRes等于某一个style资源name

     

    比如: a =context.obtainStyledAttributes(null, R.styleable.Tip, 0,R.style.tipStyle);表示使用style中名字为tipStyle的Style资源。

    <stylename="tipStyle">

            <item name="left_icon">@drawable/comment_btn</item>

            <item name="tiptextSize">8sp</item>

            <item name="close_icon">@drawable/btn_delete</item>

            <item name="bgcolor">#FF5601</item>

    </style>

    这时候不需要在theme中引用。

     

    b)        defStyleRes等于0,表示没有默认值。

     

     

    当最后两个参数都为0的时候该方法就和obtainStyledAttributes (AttributeSet set, int[] attrs)一样了。查看源码会发现obtainStyledAttributes(AttributeSet set, int[] attrs)里面调用的就是

    obtainStyledAttributes(AttributeSet set, int[] attrs, 0, 0)。这种情况就去系统theme中寻找合适的值,这个时候和第一种obtainStyledAttributes(int[] attrs) 情况相同,需要在theme对应的style下为每一个属性赋值。

    <stylename="My.Theme.dalancon" parent="@style/My.Theme">

     <item name="vpiTabPageIndicatorStyle">@style/My.TabPageIndicator</item>

     <itemname="left_icon">@drawable/comment_btn</item>

      <itemname="tiptextSize">8sp</item>

     <itemname="close_icon">@drawable/btn_delete</item>

     <item name="bgcolor">#FF5601</item>

    </style>

  • 相关阅读:
    Apache虚拟目录的建立
    自制户外登山地图傻瓜书
    经纬度与高克投影转换代码(VB)
    2000国家大地坐标系
    js格式化 Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 及相互转化
    Javascript农历与公历相互转换
    Numpy
    日期多选插件Kalendae.js
    Scrapy项目实战
    bootstrapdatetimepicker添加支持显示农历节假日信息。
  • 原文地址:https://www.cnblogs.com/xiaorenwu702/p/5178379.html
Copyright © 2020-2023  润新知