gpt4 book ai didi

Android 5.0 Lollipop : setColorFilter "leaks" onto other buttons

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:21:28 24 4
gpt4 key购买 nike

我正在使用 setColorFilter 来设置我的其中一个按钮的颜色过滤器。在 Android 5.0 Lollipop 更新之前,这一直运行良好。现在,即使我关闭 Activity 并重新打开,滤色器似乎也会泄漏到我的其他按钮上(如果我关闭应用程序并重新打开,它会重置)。

我的 styles.xml (v21):(与旧版本相同,只是它的父级是 Material,之前是 Holo)

<style name="Theme.FullScreen" parent="@android:style/Theme.Material.Light.NoActionBar.Fullscreen">
<item name="android:buttonStyle">@style/StandardButton</item>
<item name="android:windowTranslucentStatus">true</item>
</style>

我的 styles.xml(适用于所有版本):

<style name="StandardButton" parent="android:style/Widget.Button">
<item name="android:background">@android:drawable/btn_default</item>
</style>

我的按钮:

<Button
android:id="@+id/mainMenuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="mainMenu"
android:text="@string/button_mainMenu"
android:visibility="gone" />

我的代码:

Button mainMenuButton = (Button) findViewById(R.id.mainMenuButton);
mainMenuButton.getBackground().setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setVisibility(View.VISIBLE);

颜色:

<color name="light_green">#5CD65C</color>

结果:

我打开应用程序,然后游戏 Activity 和所有按钮都正确显示。我按下按钮设置滤色器,返回主菜单并重新打开游戏 Activity ,现在所有按钮都是绿色的。

有什么想法吗?

最佳答案

问题是背景 Drawable 在许多 View 中重复使用。为确保 Drawable 不在多个 View 之间共享,您应该使用 mutate 方法。

参见:mutate()

示例代码:

Drawable background = mainMenuButton.getBackground();
background.mutate();
background.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.light_green), PorterDuff.Mode.MULTIPLY));
mainMenuButton.setBackground(background);

关于Android 5.0 Lollipop : setColorFilter "leaks" onto other buttons,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27015811/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com