【问题标题】:Change Background Color From Drawable Button从可绘制按钮更改背景颜色
【发布时间】:2017-01-31 09:25:01
【问题描述】:

我有这个round_button.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="@color/colorPrimary"></solid>
            <corners android:radius="3dp"></corners>
        </shape>
    </item>

</selector>

我将那个drawable用于我的两个按钮:

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Button_Register"
            android:background="@drawable/round_button"
            android:textColor="@color/formLabelTextColor"
            android:text="@string/loginform_buttonregister" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/Button_Login"
            android:background="@drawable/round_button"
            android:textColor="@color/komWhite"
            android:text="@string/loginform_buttonlogin" />

我的问题是……

如何在java中更改Button_Register背景颜色?我试着用这个:

    Button button_register = (Button) findViewById(R.id.Button_Register);
    button_register.setBackgroundColor(R.color.formBackgroundColor);

它会移除可绘制的形状。我只想更改该特定按钮的颜色。但如何?

谢谢。

【问题讨论】:

  • 虽然对于 TextViews,我认为这符合您的需求:- (GradientDrawable) view.getBackground()).setColor(color);

标签: android


【解决方案1】:
public static void setDrawableFilterColor(Context context, int colorResource, Drawable drawable) {
    //noinspection ResourceType
    int filterColor = Color.parseColor(context.getResources().getString(colorResource));
    drawable.setColorFilter(new PorterDuffColorFilter(filterColor, PorterDuff.Mode.MULTIPLY));
}

并用

调用它
setDrawableFilterColor(mContext, R.color.colorPrimary, button_register.getBackground())

【讨论】:

    【解决方案2】:

    你可以简单地用GradientDrawable这样的东西改变你的xml drawable的背景颜色

    GradientDrawable gradientDrawable = (GradientDrawable)button_register.getBackground();
    gradientDrawable.setColor(R.color.formBackgroundColor);
    

    【讨论】:

      【解决方案3】:

      像这样在选择器文件中的 shape 属性上设置可绘制颜色

      <item android:drawable="@color/yourcolor">
      

      完整代码

      <?xml version="1.0" encoding="utf-8"?>
       <selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@color/black">
          <shape android:shape="rectangle">
              <solid android:color="@color/colorPrimary"></solid>
              <corners android:radius="3dp"></corners>      
          </shape>
      </item>
      </selector>
      

      【讨论】:

      • 嗨,兄弟,您的代码将所有按钮更改为相同颜色。我需要的是如何将新的背景颜色应用到按钮中。不是所有的按钮。
      • 您必须为该特定按钮创建单独的 xml。为按钮创建单独的文件。
      【解决方案4】:

      您可以通过在运行时使用您需要的颜色创建可绘制对象来做到这一点。以下代码将帮助您以编程方式制作可绘制图形 -

      Button button = (Button) findViewById(R.id.btn_login);
      final GradientDrawable gradientDrawable = new GradientDrawable();
      gradientDrawable.setShape(GradientDrawable.RECTANGLE);
      gradientDrawable.setCornerRadius(10);
      gradientDrawable.setColor(Color.BLUE);
      button.setBackground(gradientDrawable);
      

      希望对你有帮助。

      【讨论】:

        【解决方案5】:

        根据 sasikumar 的回答,这是可用于按钮的矩形代码,请注意:

        <solid android:color="@color/colorWhite"></solid>
        

        drawable的完整代码是:

        <?xml version="1.0" encoding="UTF-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_square">
            <stroke android:width="1dp" android:color="#3f3f44" />
            <padding android:left="2dp"
            <solid android:color="@color/colorWhite"></solid>
            android:top="2dp"
                android:right="2dp"
                android:bottom="2dp" />
            <corners android:radius="10dp" />
        </shape>
        

        这就是我实现它的方式:

         <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:layout_marginEnd="5dp"
            android:background="@drawable/buttonsquare"
            android:text="Button text"
            android:layout_marginStart="5dp">
         </Button>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-06-04
          • 1970-01-01
          • 2021-06-22
          • 2015-04-03
          • 1970-01-01
          相关资源
          最近更新 更多