颜色/可绘制的更改将应用​​于具有相同背景(颜色)的所有视图[棉花糖] [英] Color/drawable changes are applied to all views with the same background (color) [Marshmallow]

查看:102
本文介绍了颜色/可绘制的更改将应用​​于具有相同背景(颜色)的所有视图[棉花糖]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近将手机更新为Android Marshmallow并在其上运行了我现有的应用程序,但发现颜色行为有所不同:将更改应用于视图(可绘制)的背景时,所有共享相同背景的视图(参考)也将应用相同的更改.以前并非如此.

I've recently updated my phone to Android Marshmallow and ran my existing app on it, but noticed a difference in color behavior: When applying changes to the background of a view (drawable), all views that share the same background (reference) will also the same changes applied. While previously, this was not the case.

示例
在此示例中,我有两个具有相同背景色的视图,并且想要更改两个视图之一的Alpha级别.

Example
In this example, I have a two views with the same background color, and I want to change the alpha level of one of both views.

首先,我们在布局中定义视图:

First we define the views in the layout:

    <LinearLayout
        android:id="@+id/test1"
        android:orientation="horizontal"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/testColor2">

    </LinearLayout>

    <LinearLayout
        android:id="@+id/test2"
        android:orientation="horizontal"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/testColor1"
        android:layout_marginLeft="5dp">
    </LinearLayout>

两个视图共享相同的背景色或可绘制背景:

Both views share the same background color or drawable:

<color name="testColor1">#3F51B5</color>
<color name="testColor2">#3F51B5</color>

结果如下:

现在,我们将更改两个背景之一,就像这样:

Now we are going to change one of the two background, like this:

    LinearLayout test1 = (LinearLayout) findViewById(R.id.test1);
    LinearLayout test2 = (LinearLayout) findViewById(R.id.test2);
    test1.getBackground().setAlpha(80);

这将导致以下结果:

Which results in this:

但是,期望的结果显然是这样的:

However, the desired and expected result is obviously this:

此处下载示例项目.

Download the sample project here.

一些注意事项:

  • 设置Alpha级别槽XML时,此行为不适用.
  • 两个视图是否在colors.xml中引用不同的颜色定义都没有关系(如示例中所示),直接在视图的xml文件中引用两个具有相同颜色(十六进制)的相同颜色定义./li>
  • When setting the Alpha level trough XML, this behavior does not apply.
  • It does not matter if both views refer to a different color definition in colors.xml (like in the example), refer to the same color definition of both have the same color (hex) directly in the view's xml file.

问题
如何更改视图的背景,而又不影响共享相同背景的其他视图.最好同时仍能使用直接引用该颜色的xml文件中定义的颜色的背景

推荐答案

每个视图的背景和常量状态的类很可能是相同的对象.好像这两种颜色资源已经合并"某处-表示他们共享了ConstantState.也许在资源类的缓存?我本来希望他们留下来分开,因为它们是不同的资源(尽管具有相同的颜色值),但显然没有.

Most likely the class of each view's background and constantstate are the same object. It seems as if the two color resources have been "merged" somewhere -- meaning they have shared ConstantState. Maybe in the Resources class' caching? I would've expected them to stay separate since they're different resources (albeit with the same color value), but apparently not.

Snild Dolkow

ColorDrawable的状态存储Alpha,因此对一个的任何更改都会更改其他的.为防止这种情况,您可以先在可绘制对象上调用mutate(),将两个可绘制对象分开(通过复制状态).

The ColorDrawable's state stores alpha, so any changes to one will change the others. To prevent this, you can first call mutate() on the drawable, separating the two drawables (by making a copy of the state).

在示例中,这将导致使用 test1.getBackground().mutate().setAlpha(80); 而不是直接应用Alpha.

In the example, this would result in using test1.getBackground().mutate().setAlpha(80); instead of directly applying the alpha.

这篇关于颜色/可绘制的更改将应用​​于具有相同背景(颜色)的所有视图[棉花糖]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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