2010-10-23 84 views
5

我試圖爲appwidget定義主題,並將它應用於應用程序級別的 。我有一個主題一樣,將應用主題應用到appwidget

<style name="theme.dark"> 
    <item name="android"background">#000000</item> 
</style> 
在我的清單

,我在應用程序設置android:theme="@style/theme.dark"。但是,當我運行appwidget時,它不會從樣式中拾取項目。我試圖設置style="@style/theme.dark"在我的視圖佈局中的一個單獨的元素,這確實有效......但這不是我想要的。我不想爲我的視圖中的每個元素調出特定的樣式=「...」。此頁面,

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

具有使用主題/風格的一個很好的例子應用程序,它完美的作品。唯一的區別是它是一個應用程序..它將設置活動的主題,而不是應用程序。

我也嘗試使用appWidget的onHandleUpdate()中的setTheme(...)在視圖訪問前以編程方式設置上下文對象的主題。那也不管用。

有什麼想法? 謝謝。

回答

5

答案是你不能動態地將一個主題應用到一個appwidget。除了提供多個靜態引用特定主題的佈局之外,沒有任何解決方案,然後在構建遠程視圖時選擇正確的主題/佈局。

0

使用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> 
+1

我不明白這比僅僅選擇一個佈局文件或另一個更好...這是同一件事,但在一個數據文件中。 – 2011-02-18 22:08:36

+0

哦,你會如何做到這一點,你能舉個例子嗎? – 2011-02-19 00:36:43

1

既然我們不能用主題動態到appwidget,我建議在下一個簡單的解決方案 - 只是佈局文件之間切換:

,我們有兩個不同佈局:

  • layout1.xml
  • layout2.xml

我們建立我們的佈局如下:

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

當我們需要我們通過切換到第二個:

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

這個解決方案工作正常進行我。

+1

這就是我最後做的事情,但是當你開始使用2個港口和陸地佈局時,n個主題,不同屏幕尺寸的時間n佈局,這是不可管理的。使佈局得到意味着仔細應用相同的變化到每個不同的佈局。 – 2012-05-23 22:11:43

+0

對於我們將擁有不同大小的主題的情況,您絕對正確。我相信那裏有一些tweeks :) – Jviaches 2012-05-24 11:49:30

0

使用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"會做那個按鈕,是這一活動。