【问题标题】:inflating layer-list of shapes in android在android中膨胀形状的图层列表
【发布时间】:2013-08-03 12:55:45
【问题描述】:

我的可绘制文件夹中有以下文件,名为/drawable/mybkg.xml。我想给它充气,这样我就可以以编程方式更改颜色。这在 Android 中可行吗?

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item>
        <shape android:shape="oval" >
            <padding
                android:top="5dp"
                android:bottom="2dp"
                android:left="2dp"
                android:right="2dp" />

            <solid
                android:angle="270"
                android:color="#1bd4f6" />

            <stroke
                android:width="2dp"
                android:color="#121ce5" />
        </shape>
    </item>

    <!-- Foreground -->
    <item>
        <shape android:shape="oval" >

            <solid android:color="#1b90f6" />
        </shape>
    </item>

</layer-list>

我刚刚看到一个名为LayerDrawable 的类。有谁知道我如何通过编写代码或膨胀现有的 xml 将我的 xml 文件转换为这样的LayerDrawable

【问题讨论】:

    标签: android android-layout android-inflate layer-list


    【解决方案1】:

    您应该使用Resources.getDrawable(int id) 方法。这是文档:http://developer.android.com/reference/android/content/res/Resources.html#getDrawable(int) 它膨胀任何类型的可绘制资源。您唯一要做的就是将返回的可绘制对象转换为您需要的最具体的类型(在您的情况下为LayerDrawable)。这是一个例子:

    // calling from Activity
    LayerDrawable d = (LayerDrawable) getResources().getDrawable(R.drawable.my_drawable);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多