1

我有这个:

round_button.xml

<xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
    <shape android:shape="oval">
        <solid android:color="#dec60000"/>
        <size android:width="150dp" android:height="150dp"/>
    </shape>
</item>
<item android:state_pressed="false">
    <shape android:shape="oval">
        <solid android:color="#860000"/>
        <size android:width="150dp" android:height="150dp"/>
    </shape>
</item>

我的按钮:

 <Button
        android:id="@+id/incrementBTN"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:background="@drawable/round_button"
        android:onClick="onClick"
        android:soundEffectsEnabled="true"
        android:text="0"
        android:textSize="50sp"
        tools:ignore="HardcodedText" />

动态地,我想以编程方式更改背景颜色(在 round_button xml 中定义)。有没有办法我可以做到这一点?

4

3 回答 3

2

如果你想为你的按钮定义某些状态,你可以在xml中设置它们,而不必以编程方式进行(如果你这样做,你确实可以设置一个过滤器,但如果你有很多状态和条件IMO它会变得混乱)。

我将在这里详细说明步骤:

1)使用您想要的状态创建一个xml

您可以在具有已定义状态的可绘制文件夹中创建带有选择器的 xml。举个例子,

button_bkg.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:drawable="@drawable/bkg_is_pressed" android:state_pressed="true"/>
        <item android:drawable="@drawable/bkg_is_disabled" android:state_enabled="false"/>
        <item android:drawable="@drawable/bkg_default"/>
</selector>

我们称这个文件为 button_bkg.xml。在上面的示例中,我列出了 3 个状态:pressed、disabled 和 default,这意味着,当按下按钮时,它将假定 bkg_is_pressed 背景,并且当我将按钮设置为禁用时(在 xml 中或通过 setEnabled 以编程方式(布尔值),它将假定 bkg_is_disabled 背景。

2) 创建背景

现在您将在您定义的 xml 文件中定义您想要的背景(bkg_is_pressed、bkg_is_default、bkg_is_pressed)。在您的情况下,例如,您将采用 round_button.xml 文件中定义的每个形状,并将它们分成您为状态定义的每个 xml 文件。就我而言,我定义了一个图层列表:

bkg_is_pressed.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    <shape android:shape="rectangle">
            <corners android:radius="@dimen/button_corner_radius"/>
            <solid android:color="@color/color_alert"/>
            <stroke
                 android:width="@dimen/universal_1_pixel"
                    android:color="@color/color_gray_dark"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <corners android:radius="@dimen/button_corner_radius"/>
            <solid android:color="@color/color_mask_highlighted"/>
        </shape>
    </item>
</layer-list>

您将为每个州执行此操作。

需要注意的是,如果您要为 API 21+ 构建,您可以通过在您的 drawables-v21 文件夹中创建另一个 button_bkg.xml 文件来定义涟漪效应,如下所示:

button_bkg.xml(在您的 drawable-v21 文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/bkg_is_disabled" android:state_enabled="false" />
    <item android:drawable="@drawable/bkg_is_pressed" />

要使用波纹,您可以定义一种颜色,如下所述:

bkg_is_pressed.xml(在您的 drawable-v21 文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/color_mask_highlighted">
    <item android:drawable="@drawable/bkg_is_default" />
</ripple>

您只需将 button_bkg.xml 和 bkg_is_pressed.xml 放入您的 drawable-v21 文件夹文件中。在我的例子中,bkg_is_default 和 bkg_is_disabled.xml 对于 21+ 和 21- API 是相同的,所以我没有将它添加到我的 drawable-v21 文件夹中,我只是在 drawable 文件夹中创建了它。

我想强调的是,您仍然需要常规可绘制文件夹中的其他文件,以便具有 API 21- 的设备能够正常工作。

3)将该背景分配给您的按钮

最后,您只需为按钮定义该背景:

<Button
    ...
    android:background="@drawable/button_bkg
/>

所以你有它。这样,您无需以编程方式设置样式,只需在 xml 文件中定义所有背景(根据您的状态)。但是,如果您还喜欢以编程方式设置它们,您也可以这样做,只需使用 setBackground 并使用您定义的 xml 文件并应用您想要的状态逻辑(如果按下按钮,setBackground(bkg_is_pressed) 等等)

我希望这会有所帮助,让我知道这是否对您有用。

于 2016-10-22T21:40:26.133 回答
1

我通过设置 ColorFilter 解决了它:

Drawable mDrawable = context.getResources().getDrawable(R.drawable.balloons); 
mDrawable.setColorFilter(new PorterDuffColorFilter(0xffff00,PorterDuff.Mode.MULTIPLY));
myButton.setResource(mDrawable);
于 2016-10-22T05:20:24.450 回答
0

您可以根据需要使用的颜色从代码中构造形状,从中创建 StateListDrawable 并将其设置为按钮背景。

于 2016-10-21T19:56:47.960 回答