setColorFilter更改按钮颜色无效 [英] setColorFilter to change Button color has no effect

查看:141
本文介绍了setColorFilter更改按钮颜色无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a few buttons in my app layout, and I want to change the color of those buttons dynamically. When I use

b.setBackgroundColor(0xFF386F00);

颜色按预期变化,但是按钮的形状,大小和填充也发生变化,例如与与此相关的问题.

the color changes as expected, but the shape, size and padding of the button changes too, like in this related question.

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

I tried to use the solution suggested in that answer, but doing

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

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

has no effect on any of my buttons. I have no idea what could be the reason for this. The relevant code is this:

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;

}

这是自定义适配器的一部分,这可能会使事情复杂化,但是我也尝试了在正常"按钮上尝试过的所有操作,但都没有得到积极的结果.适配器中的其他所有功能也完全符合我的期望.

This is part of a custom adapter, which might complicate matters, but I also tried everything I tried on a "normal" button as well, without positive results. Also everything else in the adapter does exactly what I would expect.

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

This is the button part of the corresponding xml file

<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,没有任何填充,也没有圆角.我正在尝试寻找一种方法来仅更改颜色并使其余部分保持原样.

This button is for some reason rendered as a small square (!), with some space around it, and the corners are rounded. I don't know why that is, but I was quite happy with the way it looks. If setting the background color, the size changes to actual 30dpx40dp, without any padding, and no rounded corners. I am trying to find a way to just change the color and leave the rest as it is.

为什么ColorFilter不起作用?

Why is the ColorFilter not working?

如何在不改变按钮颜色的情况下改变按钮的颜色(例如尺寸等)?

How can I color my button without changing anything else about this button (like size etc.) as a side effect?

推荐答案

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

You haven't set the filtered drawable as the button's background:

@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;
}

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

But, if I can be honest, I don't understand the first part of your question:

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

the color changes as expected, but the shape, size and padding of the button changes too

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

I think that your problem is that you are using an AppCompat theme so Button is changed to AppCompatButton.

在这种情况下,如果要获得该结果,必须使用 setBackgroundTintList(ColorStateList tint)方法.可以在此处

In this case you have to use the setBackgroundTintList(ColorStateList tint) method if you want to achieve that result. Official documentation can be found here

编辑

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

I see now that your button doesn't have a background, you can't use a ColorFilter to change it since the Drawable is null.

这篇关于setColorFilter更改按钮颜色无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆