您的位置:首页 > 移动开发 > Android开发

android Theme使用三

2016-03-28 00:36 525 查看
原文链接

☆ 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)

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

说明一下最后两个参数:

defStyleAttr

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

<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,表示没有默认值,

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息