简体   繁体   English

有没有一种方法可以将alpha属性设置为子视图和父视图?

[英]Is there a way to set alpha attribute to child views as well as to parent view?

I am developing app where I have set custom drawable to main layout with alpha attribute. 我正在开发应用程序,其中我已将自定义可绘制对象设置为具有alpha属性的主布局。 But it sets alpha to whole view. 但这将alpha设置为整个视图。 and I wanted to set alpha to the only main layout. 我想将alpha设置为唯一的主要布局。 so that the text which will displayed normally without alpha effect. 这样可以正常显示的文本没有alpha效果。 Present Output 当前输出

The alpha effect is taken by whole view. alpha效果是从整个角度来看的。

<android.support.constraint.ConstraintLayout
        android:id="@+id/containerLinear"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient_with_bottom_curve"
        android:orientation="horizontal"
        android:alpha="0.4"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <ScrollView
            android:id="@+id/mainScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <LinearLayout
                android:id="@+id/scrollLinear"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">
                <RelativeLayout
                    android:id="@+id/mainRelative"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:id="@+id/txtHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="Business Name"
                        android:gravity="center"
                        android:layout_marginTop="10dp"
                        android:padding="5dp"
                        android:textStyle="bold"
                        android:textSize="20sp"
                        android:textColor="@color/logo_orange"/>
                    <RelativeLayout
                        android:id="@+id/paraOneRelative"
                        android:layout_below="@+id/txtHeading"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:id="@+id/txtParaOneBold"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Some write up"
                            android:gravity="start"
                            android:layout_marginTop="10dp"
                            android:padding="5dp"
                            android:textStyle="normal"
                            android:textSize="12sp"
                            android:textColor="@color/black"/>
                    </RelativeLayout>
                </RelativeLayout>
            </LinearLayout>
        </ScrollView>
    </android.support.constraint.ConstraintLayout>

You need to set the drawable on a view at the same level with the ScrollView 您需要在与ScrollView处于同一级别的视图上设置可绘制对象

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/containerLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/gradient_with_bottom_curve"
        android:alpha="0.4"/>

    <ScrollView
        android:id="@+id/mainScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

And you'll see that the text inside the TextViews no longer has the alpha. 并且您将看到TextViews中的文本不再具有alpha。

Not related to the alpha issue, but the ConstraintLayout is useless here. 与alpha问题无关,但是ConstraintLayout在这里没有用。 Same for the constraints you set on it. 对您设置的约束也是如此。 The constraints should go on elements inside the ConstraintLayout . 约束应该放在ConstraintLayout内部的元素上。 Basically you should have something like this: 基本上,您应该具有以下内容:

FrameLayout
  ScrollView
    ConstraintLayout
      Views

you can try set background for scroll view and try alpha there 您可以尝试为滚动视图设置背景并在那里尝试使用alpha

<RelativeLayout
                    android:id="@+id/mainRelative"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:alpha="0.4"
                    android:background="@drawable/gradient_with_bottom_curve">

Create an ImageView as a child of ConstraintLayout 创建一个ImageView作为ConstraintLayout的子级

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient_with_bottom_curve"
    android:alpha="0.4"/>

You could use FrameLayout as the base instead of ConstraintLayout and layer the ScrollView on top of a View (which has the translucent background). 您可以将FrameLayout用作ConstraintLayout的基础,并将ScrollView置于View (具有半透明背景)的顶部。 Something along the following lines should work, 遵循以下思路应该可以,

<FrameLayout
    android:id="@+id/containerLinear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <View
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:alpha="0.4"
        android:background="@drawable/gradient_with_bottom_curve" />

    <ScrollView
        android:id="@+id/mainScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:id="@+id/scrollLinear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RelativeLayout
                android:id="@+id/mainRelative"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <TextView
                    android:id="@+id/txtHeading"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Business Name"
                    android:gravity="center"
                    android:layout_marginTop="10dp"
                    android:padding="5dp"
                    android:textStyle="bold"
                    android:textSize="20sp"
                    android:textColor="@color/logo_orange"/>
                <RelativeLayout
                    android:id="@+id/paraOneRelative"
                    android:layout_below="@+id/txtHeading"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/txtParaOneBold"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Some write up"
                        android:gravity="start"
                        android:layout_marginTop="10dp"
                        android:padding="5dp"
                        android:textStyle="normal"
                        android:textSize="12sp"
                        android:textColor="@color/black"/>
                </RelativeLayout>
            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
</FrameLayout>

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

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