【问题标题】:setColorFilter to change Button color has no effectsetColorFilter 改变按钮颜色没有效果
【发布时间】:2016-02-16 20:54:54
【问题描述】:

我的应用布局中有几个按钮,我想动态更改这些按钮的颜色。当我使用

b.setBackgroundColor(0xFF386F00);

颜色会按预期变化,但按钮的形状、大小和填充也会发生变化,就像在this 相关问题中一样。

我尝试使用该答案中建议的解决方案,但这样做

b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);

对我的任何按钮都没有影响。我不知道这可能是什么原因。相关代码是这样的:

import android.widget.Button;

...

@Override
public View getView(int i, View convertView, ViewGroup parent) {
    LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);

    Button b = (Button) rowView.findViewById(R.id.HRlistB);
    b.setOnClickListener(onBButtonClicked);
    b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);
    rowView.setTag(values.get(i).getId());

    TextView textView = (TextView) rowView.findViewById(R.id.HRlisttext);
    textView.setText(values.get(i).toTitle());

    return rowView;

}

这是自定义适配器的一部分,这可能会使事情复杂化,但我也尝试了我在“正常”按钮上尝试的所有方法,但没有得到积极的结果。适配器中的其他所有内容也完全符合我的预期。

这是对应xml文件的按钮部分

<Button
    android:id="@+id/HRlistB"
    android:layout_width="30dp"
    android:layout_height="40dp"
    android:layout_above="@+id/HRlist4"
    android:layout_toLeftOf="@+id/HRlistB2"
    android:layout_toStartOf="@+id/HRlistB3"/>

此按钮出于某种原因呈现为一个小正方形 (!),其周围有一些空间,并且边角是圆角的。我不知道为什么会这样,但我对它的外观非常满意。如果设置背景颜色,大小变为实际 30dpx40dp,没有任何填充,也没有圆角。我正在尝试找到一种方法来更改颜色并保持其余部分不变。

为什么 ColorFilter 不工作?

如何在不改变按钮的任何其他内容(如大小等)的情况下为按钮着色?

【问题讨论】:

  • 你的按钮的背景是什么?
  • 我不确定我是否理解您的意思...您要我发布 xml 文件吗?
  • the color changes as expected, but the shape, size and padding of the button changes too 那么按钮的形状、大小和填充是什么?
  • 您的按钮没有背景,那么您想如何更改其颜色过滤器?

标签: android button


【解决方案1】:

您尚未将过滤后的可绘制对象设置为按钮的背景:

@Override
public View getView(int i, View convertView, ViewGroup parent) {
    LayoutInflater inflater =
            (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.hrlistitems, parent, false);

    Button b = (Button) rowView.findViewById(R.id.HRlistB);
    b.setOnClickListener(onBButtonClicked);
    Drawable buttonBackground = b.getBackground();
    if(buttonBackground != null) {
        buttonBackground.setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);
        b.setBackground(buttonBackground);
    }

    return rowView;
}

但是,老实说,我不明白你问题的第一部分:

颜色按预期变化,但按钮的形状、大小和填充也发生变化

我认为您的问题是您使用的是AppCompat 主题,因此将Button 更改为AppCompatButton

在这种情况下,如果您想获得该结果,您必须使用setBackgroundTintList(ColorStateList tint) 方法。 官方文档可以找到here

编辑

我现在看到您的按钮没有背景,您不能使用 ColorFilter 更改它,因为 Drawable 为空。

【讨论】:

  • 我认为您对AppCompat 主题的看法可能是正确的,我该如何检查? (我是 android 编程新手...)
  • @fifaltra 转到 res -> values -> styles.xml 并检查您的主题是否继承自 Theme.AppCompat
  • 啊,好的,所以:是的,我正在使用 appcompat 主题。现在我想弄清楚如何使用这个颜色状态列表:D
  • 嗯,现在我得到了这个:java.lang.NoSuchMethodError: android.widget.Button.setBackgroundTintList,这很容易解释还是我应该问一个新问题?
猜你喜欢
  • 2016-11-13
  • 1970-01-01
  • 2015-10-29
  • 2018-09-02
  • 2018-07-30
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多