相关疑难解决方法(0)

如何从drawable引用样式属性?

我想为我的应用程序提供2个可选主题.为了做到这一点,我定义了一些属性,如下所示:

 <attr format="color" name="item_background" />
Run Code Online (Sandbox Code Playgroud)

然后,我创建了两个主题,如下所示:

  <style name="ThemeA">
     <item name="item_background">#123456</item>
 </style>

 <style name="ThemeB">
     <item name="item_background">#ABCDEF</item>
 </style>
Run Code Online (Sandbox Code Playgroud)

这种方法效果很好,允许我轻松地创建和修改几个主题.问题是它似乎只能在Views中使用,而不能在Drawables中使用.

例如,引用布局内View的值可以:

 <TextView android:background="?item_background" />
Run Code Online (Sandbox Code Playgroud)

但是在Drawable中做同样的事情并不是:

 <shape android:shape="rectangle">
     <solid android:color="?item_background" />
 </shape>
Run Code Online (Sandbox Code Playgroud)

运行应用程序时出现此错误:

    java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
Run Code Online (Sandbox Code Playgroud)

如果不是?item_background我使用硬编码颜色,它可以工作,但这不允许我使用我的主题.我也试过?attr:item_background,但同样的事情发生了.

我怎么能这样做?为什么它在Views中有效但在Drawables中无效?我在文档中的任何地方都找不到这个限制......

android android-theme android-drawable android-attributes

87
推荐指数
4
解决办法
3万
查看次数

Android:如何在代码中获取属性的值?

我想在代码中检索textApperanceLarge的int值.我相信下面的代码是正确的方向,但无法弄清楚如何从TypedValue中提取int值.

TypedValue typedValue = new TypedValue(); 
((Activity)context).getTheme().resolveAttribute(android.R.attr.textAppearanceLarge, typedValue, true);
Run Code Online (Sandbox Code Playgroud)

android

77
推荐指数
4
解决办法
7万
查看次数

如何从drawable引用到样式

带有标签的我的应用有两个主题.在每个主题中,选项卡具有处​​于选定和未选定状态的不同图像.我如何按主题正确引用图像?

例如.我有themes.xml

<?xml version="1.0" encoding="utf-8"?>

<style name="LightTheme" parent="@android:style/Theme.Light">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_light</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_light</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_light</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_light</item>
</style>

<style name="DarkTheme" parent="@android:style/Theme.Black">
    <item name="tabShows">@drawable/ic_tab_shows_unselected_dark</item>
    <item name="tabShowsSelected">@drawable/ic_tab_shows_selected_dark</item>
    <item name="tabNews">@drawable/ic_tab_news_selected_dark</item>
    <item name="tabNewsSelected">@drawable/ic_tab_news_unselected_dark</item>
   </style>
Run Code Online (Sandbox Code Playgroud)

我还有一个tab_shows.xml和tab_news.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="@drawable/ic_tab_shows_selected_light"/>
<item  android:state_selected="false" android:drawable="@drawable/ic_tab_shows_unselected_light" />
Run Code Online (Sandbox Code Playgroud)

如何根据当前主题在选择器中引用所需的图像?这不适合我

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item  android:state_selected="true" android:drawable="?tabShowsSelected"/>
<item  android:state_selected="false" android:drawable="?tabShows" />
Run Code Online (Sandbox Code Playgroud)

在布局文件中这是有效的,我的意思是通过?styleName引用样式

android themes styles image reference

8
推荐指数
2
解决办法
1万
查看次数