【问题标题】:Is there a way to set alpha attribute to child views as well as to parent view?有没有办法将 alpha 属性设置为子视图以及父视图?
【发布时间】:2019-08-23 08:25:35
【问题描述】:

我正在开发应用程序,我已将自定义可绘制对象设置为具有 alpha 属性的主布局。但它将 alpha 设置为整个视图。我想将 alpha 设置为唯一的主要布局。使正常显示的文本没有alpha效果。 Present Output

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>

【问题讨论】:

    标签: android view xml-drawable


    【解决方案1】:

    您可以使用FrameLayout 作为基础而不是ConstraintLayout,并将ScrollView 叠加在View(具有半透明背景)之上。以下几行应该可行,

    <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>
    

    【讨论】:

      【解决方案2】:

      您可以尝试为滚动视图设置背景并在那里尝试 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">
      

      【讨论】:

        【解决方案3】:

        您需要在与 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">
        

        您会看到TextViews 中的文本不再具有alpha。

        与 alpha 问题无关,但 ConstraintLayout 在这里没用。您在其上设置的约束也是如此。约束应该在ConstraintLayout 内的元素上进行。基本上你应该有这样的东西:

        FrameLayout
          ScrollView
            ConstraintLayout
              Views
        

        【讨论】:

        • 好的。我做到了,但是两个 textviews 仍然采用 alpha 效果。我想将它设置为drawable
        • @mi5pune 我添加了一些我的意思的代码示例。您会看到文本不再具有 alpha
        【解决方案4】:

        创建一个 ImageView 作为 ConstraintLayout 的子级

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-06-23
          • 1970-01-01
          相关资源
          最近更新 更多