【问题标题】:How to set the background color of a button using a layout as the background如何使用布局作为背景设置按钮的背景颜色
【发布时间】:2020-12-08 23:48:38
【问题描述】:

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)设置背景颜色,那么圆角就没有了。

我假设是因为它将背景属性覆盖为纯色值而不是layout_bg.xml 文件。

如何设置此特定 ImageButton 的背景颜色,而不是我的 main_activity 中使用 layout_bg 作为背景的所有 ImageButtons?

【问题讨论】:

  • 你需要用红色制作另一个drawable然后使用它:btnFoo.SetBackgroundResource(R.drawable.red_drawable);
  • @WowoOt 不理想,但有效。它比 MaterialButton 答案在下面给我的问题提供了更快的解决方案,因此如果您希望我接受,请发布答案。
  • @John 你不需要创建另一个drawable。只需申请colorFilter。检查answer below

标签: java android android-layout xamarin android-button


【解决方案1】:

您需要使用红色制作另一个可绘制对象,然后使用它:

btnFoo.setBackgroundResource(R.drawable.red_drawable);

【讨论】:

    【解决方案2】:

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

        <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"/>
    

    【讨论】:

      【解决方案3】:

      你可以使用类似的东西:

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

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

      您也可以使用MaterialButton

         <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"/>
      

      在这种情况下,只需使用:

      button1.backgroundTintList = ContextCompat.getColorStateList(this, R.color....)
      

      【讨论】:

        猜你喜欢
        • 2015-02-14
        • 1970-01-01
        • 1970-01-01
        • 2019-06-07
        • 1970-01-01
        • 1970-01-01
        • 2016-01-26
        相关资源
        最近更新 更多