我有,我想设置方案中的绘制对象
根据主题定义。
要进一步说明这一点,这是我在code:
\水库\值\ attrs.xml
<资源>
<申报,设置样式名称=AppTheme>
< attr指示NAME =homeIcon格式=参考/>
< /申报,设置样式>
< /资源>
资源\值\ styles.xml
<资源>
<样式名称=AppTheme父=安卓风格/主题>
<项目名称=ATTR / homeIcon> @可绘制/ ic_home< /项目>
< /风格>
< /资源>
AndroidManifest.xml中
<应用机器人:标签=@字符串/ APP_NAME
机器人:主题=@风格/ AppTheme>
<活动机器人:MainActivity名称=机器人:标签=@字符串/ APP_NAME>
<意向滤光器>
<作用机器人:名称=android.intent.action.MAIN/>
<类机器人:名称=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT;
< /用途>
所以当你已经注意到我定义了一个自定义的ATTR homeIcon和设置在AppTheme的属性值。
当我定义这个属性在布局XML,并尝试它工作顺利进行访问
< ImageView的机器人:ID =@ + ID / IMG
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_gravity =中心
机器人:SRC =?ATTR / homeIcon/>
和呈现绘制对象
ic_home在的ImageView
。
但我无法弄清楚如何访问绘制对象
编程。
我试图做一个变通,通过定义一个支架 LayerList绘制对象
,这将导致资源未发现异常:
< XML版本=1.0编码=UTF-8&GT?;
<层列表
的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目
机器人:可绘制=?ATTR / homeIcon/>
< /层列表>
摘要
我要访问的绘制对象
中定义的自定义定义的主题
编程。
我想你可以用这个code中的绘制对象:
TypedArray A = getTheme()obtainStyledAttributes(R.style.AppTheme,新INT [] {} R.attr.homeIcon)。
INT attributeResourceId = a.getResourceId(0,0);
可绘制绘制= getResources()getDrawable(attributeResourceId)。
a.recycle();
I have a scenario in which I want to set a Drawable
depending upon the theme defined.
To explain this further, Here is what I have in code:
\res\values\attrs.xml
<resources>
<declare-styleable name="AppTheme">
<attr name="homeIcon" format="reference" />
</declare-styleable>
</resources>
res\values\styles.xml
<resources>
<style name="AppTheme" parent="android:style/Theme">
<item name="attr/homeIcon">@drawable/ic_home</item>
</style>
</resources>
AndroidManifest.xml
<application android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
So as you have noticed I am defining a custom attr homeIcon and setting the attribute value in AppTheme.
When I define this attribute in a layout XML and try to access it it works smoothly
<ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="?attr/homeIcon" />
and renders the Drawable
ic_home in an ImageView
.
But I am not able to figure out how to access the Drawable
programmatically.
I tried to do this with a work around, by defining a holder LayerList Drawable
, which results in resource not found exception:
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="?attr/homeIcon" />
</layer-list>
Summary
I want to access the Drawable
defined in a custom defined Theme
programmatically.
解决方案
I think you can get the Drawable with this code:
TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {R.attr.homeIcon});
int attributeResourceId = a.getResourceId(0, 0);
Drawable drawable = getResources().getDrawable(attributeResourceId);
a.recycle();