我有一个场景,我想根据定义的主题设置 Drawable
.
为了进一步解释这一点,这是我在代码中的内容:
esvaluesattrs.xml
<declare-styleable name="AppTheme"><attr name="homeIcon" format="reference"/></declare-styleable></资源>
resvaluesstyles.xml
<style name="AppTheme" parent="android:style/Theme"><item name="attr/homeIcon">@drawable/ic_home</item></风格></资源>
AndroidManifest.xml
<application android:label="@string/app_name"android:theme="@style/AppTheme"><activity android:name=".MainActivity" android:label="@string/app_name"><意图过滤器><action android:name="android.intent.action.MAIN"/><类别 android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动></应用程序>
正如您所注意到的,我正在定义一个自定义 attr homeIcon 并在 AppTheme 中设置属性值.
当我在布局 XML 中定义此属性并尝试访问它时,它工作顺利
<ImageView android:id="@+id/img"android:layout_width="wrap_content"android:layout_height="wrap_content"安卓:layout_gravity="center"android:src="?attr/homeIcon"/>
并在 ImageView
中呈现 Drawable
ic_home.
但我无法弄清楚如何以编程方式访问 Drawable
.
我试图通过定义一个持有者LayerList Drawable
来解决这个问题,这会导致找不到资源异常:
<?xml version="1.0" encoding="utf-8"?><层列表xmlns:android="http://schemas.android.com/apk/res/android" ><项目android:drawable="?attr/homeIcon"/></层列表>
<块引用>
总结我想以编程方式访问自定义 Theme
中定义的 Drawable
.
我想你可以用这段代码获得 Drawable:
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();
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:
esvaluesattrs.xml
<resources>
<declare-styleable name="AppTheme">
<attr name="homeIcon" format="reference" />
</declare-styleable>
</resources>
resvaluesstyles.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 definedTheme
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();
这篇关于访问在主题和 attrs.xml android 中定义的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!