7

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

4

1 に答える 1

7

最近、ImageViewテーマに合わせて絵柄を変えて欲しいという要望がありました。私は を使用しており、すぐに使用できるグループ インジケーターではなく、グループ インジケーターとしてExpandableListView使用していました。ImageView私のアプリケーションは明るいテーマと暗いテーマの両方をサポートしていたので、グループ インジケーター、アクション バーのアイコンも明るいバージョンと暗いバージョンに切り替えたいと思いました。

方法は次のとおりです。

res/values/attrs.xmlテーマに基づいて変更される各 ImageView または ActionBar アイコンのファイルでカスタム属性を宣言します。

<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指し、テーマはライト テーマに関連するドローアブルを指しています。

<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 アイコンの場合:

<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 の場合:

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

エキサイティングな部分は、この概念が任意のコントロールまたは任意の属性に適用できることです:)

于 2014-06-04T17:36:44.607 に答える