访问在主题和 attrs.xml android 中定义的资源

Access resource defined in theme and attrs.xml android(访问在主题和 attrs.xml android 中定义的资源)
本文介绍了访问在主题和 attrs.xml android 中定义的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个场景,我想根据定义的主题设置 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 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();

这篇关于访问在主题和 attrs.xml android 中定义的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)