简体   繁体   English

Android GradientDrawable setColor不起作用

[英]Android GradientDrawable setColor does not work

I have an annoying problem setting a color to a layout in Android; 我在Android中为布局设置颜色时遇到麻烦的问题;

This is how it looks my element in layout.xml (FrameLayout (painted element) in a LinearLayout ): 这是我在layout.xml(LinearLayout中的FrameLayout(已绘制元素))中的外观:

 <LinearLayout
    android:id="@+id/farPD_mid_linearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <!--  top margin layout-->
        <FrameLayout
          android:id="@+id/farPD_top_margin"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:background="@android:color/white">
        </FrameLayout>

I want to paint the FrameLayout in white and I do something like this in Java: 我想将FrameLayout 白色,然后在Java中执行以下操作:

    GradientDrawable gd3 = new GradientDrawable();
    gd3.setColor(Color.parseColor("#ffffff"));
    FrameLayout topBorder = (FrameLayout) findViewById(R.id.farPD_top_margin);
    topBorder.setBackground(gd3);

I also tried with setColors(). 我也尝试了setColors()。 Alpha is 255. The problem is that it doesn't paint in WHITE! Alpha是255。问题是它无法在白色中绘制! it paints in some sort of grey :|.(*and nor it's the color set in layout). 它以某种灰色:|。(*也不是布局中设置的颜色)绘制。 Plus, if i comment the Java code, and let only the color set in the layout, which is also white, I get the same output: FrameLayout painted in GREY! 另外,如果我注释Java代码,并且只让布局中设置的颜色也为白色,我将得到相同的输出:GREY中绘制了FrameLayout!

Can someone help me please? 有人能帮助我吗? Thank you! 谢谢!

You can also set gradient background using xml 您还可以使用xml设置渐变背景

gradient_bg.xml gradient_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape>
            <gradient
                android:startColor="@color/selected_blue_color"
                android:endColor="@color/default_blue_color"
                android:angle="270" />
        </shape>
    </item>
</selector>

layout.xml layout.xml

LinearLayout
    android:id="@+id/farPD_mid_linearLayout"
    android:orientation="vertical"
    android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <!--  top margin layout-->
        <FrameLayout
          android:id="@+id/farPD_top_margin"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:background="@drawable/gradient_bg">
        </FrameLayout>
</LinearLayout>

Not 100% related to this question, but I thought I'd post what I found out for anybody out there still struggling. 并非100%与该问题相关,但我认为我应该将发现的结果发布给仍然在挣扎中的任何人。 I was just having an issue with the setColors function of GradientDrawable and I stumbled upon this question hoping to find help. 我刚遇到GradientDrawablesetColors函数时遇到问题,偶然发现了这个问题,希望寻求帮助。 As you said, it wouldn't take the colors that I was giving it and would choose something intermediate. 正如您所说,它不会采用我给它的颜色,而是会选择一些中间的颜色。 In my scenario, I have a list of Cards in a RecyclerView , and I'm assigning a different gradient color to the background of each. 在我的场景中,我在RecyclerView有一张Cards列表,并且为每张Cards的背景分配了不同的渐变颜色。 But it always seems as if the color of one Card would propagate to the other Cards for some reason. 但是,由于某种原因,一张Card的颜色似乎总是会传播到另一张Cards

Solution

After struggling with it for a bit, I ended taking a look at the documentation (which I probably should have done first) and found out you may need to call .mutate() on any instance of GradientDrawable before making any changes to it. 在挣扎了一段时间之后,我结束了对文档的查看(我可能应该.mutate()一下),发现您可能需要在GradientDrawable任何实例上调用.mutate() ,然后再对其进行任何更改。 That's because, unless you specify that the instance is mutable, it may be linked to other existing GradientDrawable instances and the changes made to one may propagate to the others. 这是因为,除非您指定实例是可变的,否则它可能会链接到其他现有的GradientDrawable实例,并且对一个实例所做的更改可能会传播到其他实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM