代码之家  ›  专栏  ›  技术社区  ›  Jeffrey Blattman

将主题应用于appwidget

  •  7
  • Jeffrey Blattman  · 技术社区  · 15 年前

    我试图为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(…)在上下文对象上以编程方式设置主题。那也不管用。

    谢谢。

    5 回复  |  直到 15 年前
        1
  •  8
  •   Jeffrey Blattman    15 年前

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

        2
  •  3
  •   Intrications    13 年前

    由于我们不能对appwidget动态使用主题,我建议下一个简单的解决方案-只需在布局文件之间切换:

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

    • layout2.xml布局

    我们的布局如下:

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

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

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

        3
  •  0
  •   hundreAd    9 年前

    使用 style="@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 "

        4
  •  -1
  •   Roger Alien    15 年前

    使用 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>
    
        5
  •  -4
  •   mattlaabs    13 年前