本文介绍了Android:设置颜色滤镜时,按钮背景XML有时会丢失alpha的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个非常奇怪的问题,即可能是一个错误.我有一个可绘制的资源,可将其用作应用程序中按钮的背景.这是XML,非常简单:

I've run into a really strange issue that might be a bug. I've got a drawable resource that I use as the background for buttons in my app. Here is the XML, pretty simple:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:color="@color/white"
        android:width="5dp" />
    <padding
        android:left="10dp"
        android:right="10dp" />
    <corners android:radius="8dp" />
</shape>

我正在尝试让用户设置应用程序的前景色和背景色,所以这是我更新颜色的方法:

I'm experimenting with letting the user set both the foreground and background colors of the app, so this is how I update the colors:

int foreground = getSharedPreferences("prefs", MODE_PRIVATE).getInt("foreground", 0);
_buttonOptions.setTextColor(foreground);
_buttonOptions.getBackground().setColorFilter(foreground, PorterDuff.Mode.MULTIPLY);

直到几小时前,它一直运行良好.当我运行我的应用程序时,可绘制对象会给自己一个黑色的背景,这似乎是我无法摆脱的.

This was working perfectly until a few hours ago. When I ran my app, the drawable gave itself a black background that I can't seem to get rid of.

我尽了所有我能想到的东西,但是黑人留下了.我什至没有做任何可能使这种事情发生的事情.在更改期间,我唯一要做的就是将按钮的文字样式设置为粗体.我尝试将其改回来,但这没有帮助.有趣的是,我还尝试将滤色器模式更改为SRC_ATOP,并为整个按钮区域着色.就像Alpha通道完全消失了一样.

I've tried everything I can think of, but the black stays. I didn't even do anything that could have made this happen; the only thing I did around the time that it changed was set the button's text style to bold. I tried changing it back, but that didn't help. Interestingly, I also tried changing the color filter mode to SRC_ATOP, and it colored the entire button area. So it is as if the alpha channel has completely disappeared.

无论如何,我不知道为什么会这样,因此我为什么认为这可能是一个错误.你们怎么看?

Anyway, I have no clue why this is happening, hence why I think it could be a bug. What do you guys think?

仅在设置彩色滤光片时出现此问题.如果我注释掉该行,那么它可以正常工作(当然要减去我需要的颜色).

The problem only shows up when setting the color filter. If I comment that line out, it works fine (minus the coloring that I need, of course).

推荐答案

好的,我已经解决了这个问题.我必须为可绘制对象明确添加透明背景,然后清理项目.因此,XML现在看起来像这样:

Okay, I've fixed the problem. I had to explicitly add a transparent background to the drawable and then clean the project. So the XML now looks like this:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <solid android:color="@android:color/transparent" />
    <stroke
        android:color="@color/white"
        android:width="5dp" />
    <padding
        android:left="10dp"
        android:right="10dp" />
    <corners android:radius="8dp" />
</shape>

我早些时候尝试过,但是它没有做任何事情,因为我没有清理项目,所以我认为它根本没有用.

I tried this earlier, but it didn't do anything because I didn't clean the project, so I assumed that it hadn't worked at all.

这篇关于Android:设置颜色滤镜时,按钮背景XML有时会丢失alpha的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:27
查看更多