CardView中的半透明背景无法正常工作

时间:2020-04-20 10:53:22

标签: android xml android-studio android-layout

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

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

这是您想要的吗?

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

答案 2 :(得分:0)

将第一个cardView对象封装在框架布局中,然后为其赋予高度并将其保持在顶部(请记住!levation> = 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>
相关问题