应用主题,基于应用主题名称的更改和ImageView src [英] Application Themes, change and ImageView src based on app theme name

查看:150
本文介绍了应用主题,基于应用主题名称的更改和ImageView src的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图为我的问题找到解决方案,但我找不到任何地方,甚至可以谷歌搜索它.

i'm trying to find a solution for my problem, but i can't find anywhere, even googling for it.

我正在编写一个使用主题的android应用程序,用户可以对其进行动态切换,然后应用程序重新启动以应用所选主题.效果很好.

I'm writing an android application that uses themes, an user can switch them dinamically, and the app restart to apply the choosed one. This is working well.

但是,我找不到基于所选主题更改imageview元素的方法.我无法理解android应用使用主题的方式,我无法动态更改文件,而只能更改应用对象中的主题名称,并且在主题样式标签中我无法指定单个imageview的src.

But, i can't find the way to change an imageview element based on the choosed theme. I can't understand the way android apps uses themes, i can't change the file dinamically but only change the theme name in the application object, and in the theme style tag i can't specify a single imageview's src.

我也可以仅使用windowbackground属性来满足我的应用程序要求.但是,我无法使用scaletype属性按比例地拉伸,也无法在bitmapdrawable中使用重力,因此我无法理解如何使其在窗口内减小以保持图像比例.

I can satisfy my app requirements also using only the windowbackground property. But, i can't make it strech proportionally using the scaletype property, also using gravity in a bitmapdrawable i can't understand how to make it reduce inside the windows mantaining the image proportions.

感谢所有回答我的问题的人,并为我的英语感到抱歉!:)

Thanks to everyone that reply to my question, and sorry for my english! :)

推荐答案

最近,我需要更改要根据主题更改的 ImageView 图片.我正在使用 ExpandableListView 并使用 ImageView 作为组指示器,而不是开箱即用的组指示器.由于我的应用程序同时支持亮色和深色主题,因此我希望将组指示器和操作栏图标也切换为亮色和深色版本.

Recently, I had a requirement to change the ImageView picture to be changed based on the theme. I was using an ExpandableListView and using the ImageView as a group indicator rather than out of the box group indicator. Since my application supported both light and dark theme, I wanted to switch the group indicators, action bar icons to light and dark versions as well.

这里是这样:

res/values/attrs.xml 文件中声明要根据主题更改的每个ImageView或ActionBar图标的自定义属性:

Declare custom attributes in res/values/attrs.xml file for each of the ImageView or ActionBar icon to be changed based on theme:

<declare-styleable name="customAttrs">
    <attr name="actionSearchIcon" format="reference" />
    <attr name="actionAcceptIcon" format="reference" />
    <attr name="actionRefreshIcon" format="reference" />
    <attr name="groupIndicatorIcon" format="reference" />
</declare-styleable>

在主题文件中,该主题文件将为 res/values/styles.xml 或相应的主题文件,具体取决于您要定位的版本.适当的绘画.例如,在下面的示例中, Black 主题指向与深色主题相关的可绘制对象,而 Light 主题指向与浅色主题相关的可绘制对象.

In the theme file which would be res/values/styles.xml or the suitable one depending upon the version you are targetting, define the same attributes pointing to the appropriate drawables. For example below, the Black theme is pointing to drawables related to dark theme and Light theme is pointing to drawables related to light theme.

<style name="Black" parent="@style/Theme.AppCompat">
    <item name="actionSearchIcon">@drawable/ic_action_search</item>
    <item name="actionAcceptIcon">@drawable/ic_action_accept</item>
    <item name="actionRefreshIcon">@drawable/navigation_refresh</item>
    <item name="groupIndicatorIcon">@drawable/group_indicator</item>
</style>


<style name="Light" parent="@style/Theme.AppCompat.Light">
    <item name="actionSearchIcon">@drawable/ic_action_search_light</item>
    <item name="actionAcceptIcon">@drawable/ic_action_accept_light</item>
    <item name="actionRefreshIcon">@drawable/navigation_refresh_light</item>
    <item name="groupIndicatorIcon">@drawable/group_indicator_light</item>
</style>

最后,进入实际的布局, 将相关的属性指向您已定义的属性,而不是直接指向可绘制对象. 如果是ActionBar图标:

Finally, coming to the actual layouts, point the relavant attributes to the attributes you have defined rather than directly to the drawables. For example in case of ActionBar icons:

<item
android:id="@+id/action_refresh"
android:icon="?attr/actionRefreshIcon"
android:orderInCategory="107"
android:title="@string/action_refresh"
app:showAsAction="always"/>

<item
android:id="@+id/action_accept"
android:icon="?attr/actionAcceptIcon"
android:orderInCategory="108"
android:title="@string/action_accept"
app:showAsAction="always"/>

<item
android:id="@+id/action_search"
android:icon="?attr/actionSearchIcon"
android:orderInCategory="50"
android:title="@string/action_search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="collapseActionView|always"/>

或者使用ImageView:

Or in case of ImageView:

<ImageView
android:id="@+id/group_indicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:contentDescription="@string/app_name"
android:src="?attr/groupIndicatorIcon"/>

令人兴奋的部分是,该概念可以应用于任何控件或任何属性:)

The exciting part is that this concept can be applied to any control or any attribute :)

这篇关于应用主题,基于应用主题名称的更改和ImageView src的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆