【问题标题】:How to customize custom drawable defined in XML at runtime?如何在运行时自定义 XML 中定义的自定义可绘制对象?
【发布时间】:2015-02-25 00:19:36
【问题描述】:

我制作了一个自定义的多层可绘制对象作为按钮的背景。有时,我希望这个可绘制层的一部分是蓝色的。有时我希望它是绿色的。关键是,它是一个变量,我希望它可以在关联的自定义视图 XML 中定义。

这可能吗?如何在 XML 中编写可在运行时确定其值的可绘制对象?

custom_button.xml

<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="@dimen/button_inset_horizontal_material"
    android:insetTop="@dimen/button_inset_vertical_material"
    android:insetRight="@dimen/button_inset_horizontal_material"
    android:insetBottom="@dimen/button_inset_vertical_material">
    <shape android:shape="rectangle">
        <corners android:radius="@dimen/control_corner_material" />
        <solid android:color="?attr/colorButtonNormal" />
        <padding android:left="@dimen/button_padding_horizontal_material"
            android:top="@dimen/button_padding_vertical_material"
            android:right="@dimen/button_padding_horizontal_material"
            android:bottom="@dimen/button_padding_vertical_material" />
    </shape>
</inset>

&lt;solid android:color="?attr/colorButtonNormal" /&gt; 行是我想在运行时设置的。我的这个类的自定义视图已经接收到我想在这里使用的颜色值 - 我如何将它应用到这个可绘制对象的 XML 中?

【问题讨论】:

  • 不知道是否有办法在运行时以编程方式修改自定义逻辑。您可能只需要创建两个 custom_button.xml 文件并在需要时适当地选择它们。
  • @JaySnayder 我实际上正在尝试为自定义按钮视图创建一个库,让我可以轻松修改此可绘制对象中使用的值
  • 将形状提取到可绘制对象中,然后您可以更改它:stackoverflow.com/questions/7164630/…

标签: android android-layout android-drawable android-attributes


【解决方案1】:

像这样:

InsetDrawable drawable = (InsetDrawable) myButton.getBackground();
GradientDrawable shape = (GradientDrawable) drawable.getDrawable();
shape.setColor(Color.BLUE);

我制作了一个自定义的多层可绘制对象作为按钮的背景。

这假设 myButton 是您在上面提到的按钮,并且已经定义了

android:background="@drawable/custom_button"

编辑

对于执行此操作的 API 级别 1 方法:

制作一个custom_shape.xmldrawable:

<shape android:shape="rectangle">
    <corners android:radius="@dimen/control_corner_material" />
    <solid android:color="?attr/colorButtonNormal" />
    <padding android:left="@dimen/button_padding_horizontal_material"
        android:top="@dimen/button_padding_vertical_material"
        android:right="@dimen/button_padding_horizontal_material"
        android:bottom="@dimen/button_padding_vertical_material" />
</shape>

编写一个方法来改变这个drawable的颜色并在它周围放置一个插图:

private void changeColor() {
    // Get shape from XML
    GradientDrawable shape = (GradientDrawable) getResources().getDrawable(R.drawable.custom_shape);
    shape.setColor(Color.BLUE);

    // Programmatically create Inset
    InsetDrawable drawable =  new InsetDrawable(shape,
            getResources().getDimensionPixelSize(R.dimen.button_inset_horizontal_material),
            getResources().getDimensionPixelSize(R.dimen.button_inset_vertical_material),
            getResources().getDimensionPixelSize(R.dimen.button_inset_horizontal_material),
            getResources().getDimensionPixelSize(R.dimen.button_inset_vertical_material));

    // Apply to button
    myButton.setBackground(drawable);
}

【讨论】:

  • 你能再解释一下吗?这不会使用我定义的 custom_button.xml 可绘制对象
  • 是的。 getBackground() 返回您在 custom_button.xml 中创建的 InsetDrawable,前提是您已将其设置为按钮的背景,如您所述。
  • 谢谢。希望这会奏效。看起来 gradientDrawable.getDrawable() 是 KITKAT 或更高版本
  • 有没有办法使用 Inset 来做到这一点?
  • 这个库适用于 ICS 或更高版本,但 4.1 也可以。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-26
  • 2015-09-21
  • 2021-01-31
  • 2011-12-19
  • 1970-01-01
相关资源
最近更新 更多