applying a theme to an appwidget

主宰稳场 提交于 2019-12-03 13:09:52

问题


i'm trying to define a theme for an appwidget, and have it applied at the application level. i have a theme like,

<style name="theme.dark"> 
  <item name="android"background">#000000</item> 
</style> 

in my manifest, i set android:theme="@style/theme.dark" at the application. however, when i run the appwidget, it does not pick up the items from the style. i tried setting style="@style/theme.dark" on an individual element in my view layout, and that does work ... but that's not what i want. i don't want to call out a specific style="..." for each element in my view. this page,

http://www.androidengineer.com/2010/06/using-themes-in-android-applications.html

has an excellent example app for using themes / styles, and it works perfectly. the only difference is that it's an application .. and it's setting the theme on the activity, not the application.

i've also tried setting the theme at programmatically on the Context object using setTheme(...) in onHandleUpdate() of the appwidget, before the view is accessed. that doesn't work either.

any ideas? thanks.


回答1:


the answer is that you can't apply a theme dynamically to an appwidget. there's no solution other than providing multiple layouts each statically referencing a particular theme, then picking the correct theme/layout when you build your remote views.




回答2:


Since we cannot use theme dynamically to an appwidget, I suggest next simple solution - just switch between layout files:

Suppose we have two different layouts:

  • layout1.xml
  • layout2.xml

We set our layout as following:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout1);

And when we need we switch it to second one by:

RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.layout2);

This solution is working fine for me.




回答3:


Use style="@android:style/Widget.(THEME).(CONTROLTYPE)" in the relevant layout, e.g. Holo on a button:

<Button
    android:id="@+id/button1"
    style="@android:style/Widget.Holo.Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

The style= above will style a button equivalent to what setting android:theme="android:Theme.Holo" in the manifest would do to that button, were this an activity.




回答4:


Use setVisibility to hide layouts with it's own styles in background. Like this:

public static void changeWidgetState(RemoteViews remoteView, int state){
    switch (state){
        case 0:{
            remoteView.setViewVisibility(R.id.widgetLayout1, View.VISIBLE);
            remoteView.setViewVisibility(R.id.widgetLayout2, View.GONE);
        } break;
        case 1:{
            remoteView.setViewVisibility(R.id.widgetLayout1, View.GONE);
            remoteView.setViewVisibility(R.id.widgetLayout2, View.VISIBLE);
        } break;
        ...
        default:
    }

}

xml

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/widgetLayout0"
            android:layout_width="150dip"
            android:layout_height="wrap_content"

            >

        <!--Normal Theme Black Text -->
        <RelativeLayout
                android:id="@+id/widgetLayout1"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                style="@style/WidgetBackgroundNormal"

                />
        <!--Yellow Theme Black Text -->
        <RelativeLayout
                android:id="@+id/widgetLayout2"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                style="@style/WidgetBackgroundYellow"
                />
         ...

        <LinearLayout
            android:orientation="vertical"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:paddingTop="7dip"
            >
            <TextView
                android:id="@+id/widget_server_name"
                style="@style/Text.DefinitionWhite"
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_gravity="left"
                android:layout_marginLeft="10dip"
                    />
            ....
        </LinearLayout>
    </RelativeLayout>



回答5:


Using a ContextThemeWrapper should do the trick http://developer.android.com/reference/android/view/ContextThemeWrapper.html



来源:https://stackoverflow.com/questions/4005239/applying-a-theme-to-an-appwidget

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!