2013-05-19 21 views
28

我有一个可绘制的XML文件(background.xml)绘制对象XML中:由所使用的LinearLayout在运行时改变形状纯色作为背景

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item> 
     <shape> 
     ........... 
     </shape> 
    </item> 

    <item android:id="@+id/shape_id"> 
     <shape android:shape="rectangle"> 
      <solid android:color="#ffefefef" /> 
     </shape> 
    </item> 

</layer-list> 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:background="@drawable/background" 
    android:id="@+id/layout_id" 
    > 

现在我需要基于某些条件在运行时更改固体颜色shape_id。这个怎么做?

+0

不,我不认为这是可能的 – Raghunandan

+7

一切皆有可能 – AndroidGecko

回答

59

发现我:

View v = findViewById(R.id.layout_id); 

    LayerDrawable bgDrawable = (LayerDrawable)v.getBackground(); 
    final GradientDrawable shape = (GradientDrawable) bgDrawable.findDrawableByLayerId(R.id.shape_id); 
    shape.setColor(----); 
+3

你有任何想法如何实现对远程视窗widget的一样吗?谢谢。 – rxlky

+0

对不起,但我还没有机会使用RemoteView .. –

+0

@ MV1这是可能的远程视图。因为它不工作。你可以指导我http://stackoverflow.com/questions/24545857/how-to-change-remote-view-background-in-runtime?noredirect=1#comment38013228_24545857 –