2013-03-25 13 views
0

遠程佈局的修改背景繪製我有一個小窗口的佈局,可繪製背景:Android中

<RelativeLayout 
    android:id="@+id/basic_widget_layout" 
    android:background="@drawable/basic_widget_style_2" 
    android:clickable="true" > 

@drawable/basic_widget_style_2是兩種形狀的層列表。

我也有我的顯示部件的實時的調整預覽的配置活動:

<include android:id="@+id/preview_include" 
     layout="@layout/basic_widget_layout" /> 

我試圖以編程方式調整@drawable/basic_widget_style_2通過我的配置活動,並看到實時結果@+id/preview_include

在我的配置活動中,我有特定的appWidgetId啓動了我,所以我的問題是 - 是否有可能的方法來獲取屬於啓動我的活動的小部件的@drawable/basic_widget_style_2

此外,如何使<include> -ed佈局背景可繪製失效? 看來,無論我做什麼,佈局保持靜態(嘗試使用invalidate(),refreshDrawable()和我見過的更多技巧)。

回答

0

我不明白你想要在背景可繪製中改變什麼。

爲了讓您的視圖的繪製形狀,你需要一個id分配給項目包括形狀,然後做到這一點:如果你想改變形狀,例如做的顏色

//change the color to the specific needed 
LayerDrawable preview_include = (LayerDrawable)(this.findViewByID(R.id.preview_include)).getBackground(); 
final GradientDrawable preview_include_shape = (GradientDrawable) include.findDrawableByLayerId(R.id.rectange); 

這樣的:

 preview_include_shape.setColor(getResources().getColor(getResId("white", R.color.class))); 

與形狀的XML繪製對象可能是以下幾點:

<?xml version="1.0" encoding="utf-8"?> 

<!-- Up 2dp Shadow --> 
<item > 
    <shape android:shape="rectangle"> 

     <solid android:color="@color/shadow" /> 
     <corners 
      android:radius="3dp" 
      /> 
    </shape> 
</item> 

<!-- A rectange --> 
<item android:top="3px" android:id="@+id/rectange"> 
    <shape android:shape="rectangle"> 
     <corners 
      android:topLeftRadius="3dp" 
      android:topRightRadius="3dp" 
      android:bottomLeftRadius="3dp" 
      android:bottomRightRadius="3dp"/> 
      <solid android:color="@color/black" 
        /> 
    </shape> 
</item>