【问题标题】:Semi transparent background in CardView Does not work properlyCardView 中的半透明背景无法正常工作
【发布时间】:2020-08-02 20:29:11
【问题描述】:

我对 CardView 背景不透明度有疑问。我想让我的背景半透明,但我有一个奇怪的错误,一直在发生。最好的解释是我的程序截图。

Scr from displayed layout

我希望此布局全部采用相同的半透明颜色。相反,出现的是这种半透明的颜色,里面有更透明的矩形。我不知道如何从我的布局中删除这个矩形。代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginBottom="5dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="15dp"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        app:cardBackgroundColor="#66000000">


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_gravity="center">



                <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:cardCornerRadius="15dp">


                <ImageView
                    android:id="@+id/flagImageView"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:adjustViewBounds="true"
                    android:fontFamily="@font/changa_light"
                    android:scaleType="fitXY"
                    app:srcCompat="@mipmap/ic_launcher" />
                </androidx.cardview.widget.CardView>

                <TextView
                    android:id="@+id/flagTextView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="TEST"
                    android:textColor="#ffffff"
                    android:gravity="center"
                    android:fontFamily="@font/changa_light"/>

            </LinearLayout>



    </androidx.cardview.widget.CardView>
    </LinearLayout>
</RelativeLayout>

【问题讨论】:

    标签: android xml android-studio android-layout


    【解决方案1】:

    已解决!我必须将app:cardElevation="0dp 添加到第一个 CardView

    【讨论】:

      【解决方案2】:

      这是你想要的吗?

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          android:layout_width="match_parent"
          android:layout_height="312dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="24dp"
          android:layout_marginEnd="8dp"
          android:layout_marginBottom="4dp"
          android:background="?attr/selectableItemBackground"
          android:clickable="true"
          android:focusable="true"
          android:foreground="?attr/selectableItemBackground"
          app:cardBackgroundColor="@color/sisman2"
          app:cardCornerRadius="20dp"
          app:cardElevation="2dp">
      
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
      
              <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="60dp"
                  android:layout_alignParentBottom="true"
                  android:layout_marginBottom="8dp">
      
                  <TextView
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"
                      android:layout_centerInParent="true"
                      android:ellipsize="end"
                      android:gravity="center"
                      android:singleLine="true"
                      android:text="HELLO"
                      android:textSize="24sp" />
      
      
              </RelativeLayout>
      
      
              <androidx.cardview.widget.CardView
                  android:layout_width="match_parent"
                  android:layout_height="248dp"
                  android:elevation="0dp"
                  app:cardCornerRadius="20dp"
                  app:cardElevation="0dp">
      
      
              </androidx.cardview.widget.CardView>
      
          </RelativeLayout>
      
      </androidx.cardview.widget.CardView>
      

      【讨论】:

        【解决方案3】:

        将第一个 cardView 对象包含在框架布局中,然后赋予它高度并保持在顶部(记住!高度> = API 21)

                <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:background="@color/transparentColor"
                    android:elevation="3dp"
                    android:stateListAnimator="@null">
                   <!-- into-->
        
                </FrameLayout>
        

        【讨论】:

          猜你喜欢
          • 2019-01-05
          • 2018-11-23
          • 1970-01-01
          • 2018-12-20
          • 1970-01-01
          • 1970-01-01
          • 2020-07-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多