11

我正在尝试为 appwidget 定义一个主题,并将其应用于应用程序级别。我有一个主题,

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

在我的清单中,我设置android:theme="@style/theme.dark"了应用程序。但是,当我运行 appwidget 时,它不会从样式中提取项目。我尝试style="@style/theme.dark"在视图布局中设置单个元素,这确实有效……但这不是我想要的。我不想为我认为的每个元素调用特定的 style="..." 。这一页,

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

有一个使用主题/样式的优秀示例应用程序,并且运行良好。唯一的区别是它是一个应用程序..它在活动上设置主题,而不是应用程序。

在访问视图之前,我还尝试使用 appwidget 的 onHandleUpdate() 中的 setTheme(...) 以编程方式在 Context 对象上设置主题。那也行不通。

有任何想法吗?

4

4 回答 4

9

答案是您不能将主题动态应用于应用程序小部件。除了提供多个静态引用特定主题的布局,然后在构建远程视图时选择正确的主题/布局之外,没有其他解决方案。

于 2010-12-21T17:06:41.170 回答
3

由于我们不能动态地将主题用于应用程序小部件,我建议下一个简单的解决方案 - 只需在布局文件之间切换:

假设我们有两种不同的布局:

  • 布局1.xml
  • 布局2.xml

我们将布局设置如下:

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

当我们需要时,我们通过以下方式将其切换到第二个:

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

这个解决方案对我来说很好。

于 2012-05-23T20:40:00.430 回答
0

在相关布局中使用,例如按钮上的Holostyle="@android:style/Widget.(THEME).(CONTROLTYPE)"

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

如果这是一个活动,style=上面将设置一个按钮的样式,该按钮与清单中的设置对该按钮所做的设置相同。android:theme="android:Theme.Holo"

于 2017-07-14T11:26:39.153 回答
-1

用于setVisibility在背景中隐藏具有自己样式的布局。像这样:

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>
于 2011-02-16T03:22:16.490 回答