【问题标题】:Modify background drawable of remote layout in Android修改Android中远程布局的背景drawable
【发布时间】:2013-03-25 23:03:19
【问题描述】:

我有一个带有可绘制背景的小部件布局:

<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

此外,如何使&lt;include&gt;-ed 布局背景可绘制对象无效? 似乎无论我做什么,布局都保持静态(尝试使用 invalidate()、refreshDrawable() 和我见过的更多技巧)。

【问题讨论】:

    标签: android android-layout android-widget drawable remoteview


    【解决方案1】:

    我不明白你到底想在背景中改变什么。

    为了获得 View 的可绘制形状,您需要为包含该形状的项目分配一个 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 Drawable 可能如下:

    <?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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 2011-11-21
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多