Android: Nested linear layout with different alpha

2019-02-19 06:51发布

问题:

I have a rectangular LinearLayout that has some margin, some round corners and an alpha value of 0.3. Inside this layout I have 4 different layouts as I display different images in different positions.

My issue is that although the primary layout is 0.3, I want my child to be fully visible, or not affected by it's parent alpha, and I am wondering how I can please do that please? I have tried setting alpha=1 on the children layouts but it did not work. Setting it to 0 does make the children layout disappear though, so it seems I can reduce below 0.3 but not anything above the parent. Is that a bug or am I doing it wrong please?

Thank you.

回答1:

I actually figured it out! The colors are AARRGGBB, so modifying the alpha channel (AA) only affects the current background and not the children! If there is another solution, I am happy to hear it. Thanks!



回答2:

You can solve your problem with this solution. It works perfect.

parentView.getBackground().setAlpha(128); //your parent view's visibility is now %50 and child view's visibility remains same. 


回答3:

To prevent a child view from being affected by it's parent background ...

Truth: A non-alpha-255 'color' is not actually a color - it is merely a tint!

Thus: the perceived appearance of a child view background is either:
(a) The exact color specified by child.setBackgroundColor() when that color has alpha-255 or:
(b) A composite of child.setBackgroundColor() and the parent background otherwise.

To absolutely control the child background (with utter disregard the parent) you must therefore construct a third color which will be a composite of your chosen tint and your chosen alpha-255 background.

You must nominate a background! (By definition, a tint can only be rendered against a background. If not explicitly specified the theme background will eventually come into play.)

This code took months to find and is giving perfect results.

childView.setBackgroundColor(ColorUtils.compositeColors(yourTint, yourBackground);

See my answer to my own question here.



回答4:

This may be late answer

But Try this below code i hope u get what u need

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/aa"    //Your own Image in root element
android:orientation="vertical" >

  <RelativeLayout
    android:id="@+id/RLMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#CCFFFFFF" >   //Your Alpha Value

    <ImageView
        android:id="@+id/iv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="86dp"
        android:src="#FF0000" />        //Your Child Image

</RelativeLayout>