开发者

Application Themes, change and ImageView src based on app theme name

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

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.

But, i can't find the way to change an imageview element based on the choosed theme. I can't understand the way android开发者_Python百科 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.

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! :)


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.

Here it is how:

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>

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>

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"/>

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 :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜