Java 如何使用布局作为背景设置按钮的背景色

Java 如何使用布局作为背景设置按钮的背景色,java,android,android-layout,xamarin,android-button,Java,Android,Android Layout,Xamarin,Android Button,layout_bg.xml <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF"/> <!--<stroke android:width

layout_bg.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#FFFFFF"/>
    <!--<stroke android:width="3dp" android:color="#B1BCBE" />-->
    <corners android:radius="10dp"/>
    <padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>

main_activity.xml

<ImageButton
            android:background="@drawable/layout_bg"
            android:src="@drawable/myicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:tint="@android:color/black"
            android:minWidth="80.0dp"
            android:minHeight="80.0dp"
            android:padding="10px"
            android:scaleType="fitCenter"
            android:layout_alignParentBottom="true"
            android:id="@+id/btnFoo"
            android:layout_marginLeft="91.0dp" 
           />

我正在将我的
ImageButton
的背景设置为一个布局,这样我就可以拥有动态圆角,正如您在这张图中看到的那样:

但是,我需要将该按钮的背景色更改为另一种纯色,如红色

如果我使用
btnFoo.SetBackgroundColor(color.Red)
设置背景色,则圆角消失

我假设,因为它将background属性改写为纯色值,而不是
layout\u bg.xml
文件


如何设置此特定
ImageButton
的背景色,而不是我的主要活动中使用
layout\u bg
作为背景的所有ImageButton?

使用材质按钮所有功能都已内置

    <com.google.android.material.button.MaterialButton
              android:layout_width="size"
              android:layout_height="size"
              android:backgroundTint="@color/colorPrimary"
              android:text="Add Images"
              android:textAllCaps="false"
              android:textColor="@color/white"
              app:cornerRadius="20dp"
              android:layout_marginRight="10dp"
              android:layout_marginLeft="20dp"/>


您需要制作另一个红色的可绘制图形,然后使用它:

btnFoo.setBackgroundResource(R.drawable.red_drawable);

您可以使用以下内容:

    btnFoo.background.colorFilter =
        BlendModeColorFilterCompat.createBlendModeColorFilterCompat(
            color, BlendModeCompat.SRC_ATOP)

注意:它需要
androidx.core:core:1.2.0

您也可以使用
材料按钮

   <com.google.android.material.button.MaterialButton
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:insetTop="0dp"
        android:insetBottom="0dp"
        app:icon="@drawable/ic_add_24px"
        app:iconGravity="textStart"
        app:iconPadding="0dp"
        app:iconSize="48dp"
        android:backgroundTint="@color/..."
        android:textColor="#FFFFFF"
        app:cornerRadius="10dp"/>

您需要制作另一个红色的可绘制图形,然后使用它:btnFoo.SetBackgroundResource(R.drawable.Red\u drawable)@WowoOt并不理想,但很有效。这是一个比MaterialButton答案在下面给我的问题更快的解决方案,所以如果你想让我接受它,请发布答案。@John,你不需要创建另一个绘图工具。只需应用
颜色过滤器
。检查一下。
button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)