【问题标题】:CardView: Let child item overlapCardView:让子项重叠
【发布时间】:2017-04-27 19:54:56
【问题描述】:

我的 RecyclerView 有问题。 我想设计一些看起来像这样的 CardViews:

因此我需要左上角的 ImageView 与 CardView 重叠。我已经知道负边距的用法,但我的问题主要是

即使 clipToPadding 设置为 false,Cardviews 中的重叠也会被裁剪

有人有解决这个问题的办法吗?

【问题讨论】:

    标签: android xml android-cardview


    【解决方案1】:

    只需将ImageView(Twitter 图标)elevation 设置为高于CardView 海拔。

    android:elevation="10dp" 设置为ImageView 并将card_view:cardElevation="2dp" 设置为CardView

    这是一个例子:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true">
    
            <android.support.v7.widget.CardView
                android:id="@+id/card_view"
                android:layout_width="match_parent"
                android:layout_height="200dp"
                android:layout_marginTop="20dp"
                card_view:cardElevation="2dp"
                card_view:cardCornerRadius="4dp"
                card_view:cardUseCompatPadding="false"
                card_view:cardPreventCornerOverlap="false">
    
                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
    
                    <ImageView
                        android:id="@+id/image"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignParentStart="true"
                        android:layout_alignParentTop="true"
                        android:scaleType="centerCrop"
                        android:src="@drawable/sample_1" />
    
                    <TextView
                        android:id="@+id/title"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_alignBottom="@+id/image"
                        android:padding="8dp"
                        android:background="#CCFFFFFF"
                        android:text="This is simple title"
                        android:textColor="#000000" />
                </RelativeLayout>
            </android.support.v7.widget.CardView>
    
            <ImageView
                android:id="@+id/icon_play"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_marginLeft="10dp"
                android:elevation="10dp"
                android:src="@mipmap/ic_launcher"/>
        </RelativeLayout>
    
    </RelativeLayout>
    

    输出:

    希望对你有帮助~

    【讨论】:

    • 感谢您的回答,但该解决方案存在问题。它在 XML 编辑器中正常工作,但无法呈现。
    • 您可以尝试在您的设备上运行它。它会正常工作。您的卡片视图应该由另一个线性或相对布局包裹,并且还使用 vardview 上边距。否则不会显示。
    【解决方案2】:

    必须重叠 CardView 的子级将这个属性添加到子级

    注意:子标高必须大于 CardView 标高

    android:elevation="10dp"
    

    【讨论】:

      【解决方案3】:

      您可以尝试在 CardView 中创建另一个布局作为主要内容气泡,而不是图像上的负边距(可能是不可预测的),并在其中添加边距。

      <CardView>
          <FrameLayout
              background="bubble"
              marginTop="15dp">
      
              <!-- stuff here -->
      
          </FrameLayout>
      
          <ImageView ... />
      </CardView>
      

      【讨论】:

        【解决方案4】:

        在约束布局中.....我做了左边你可以根据你的要求改变。

        <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".SecondActivity">
        
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toTopOf="parent">
        
            <androidx.cardview.widget.CardView
                android:id="@+id/card"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:layout_margin="20dp"
                app:cardCornerRadius="100dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">
        
                <androidx.constraintlayout.widget.ConstraintLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
        
                    <ImageView
                        android:layout_width="150dp"
                        android:layout_height="150dp"
                        android:layout_margin="20dp"
                        android:src="@drawable/ic_box"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
        
        
                        />
        
                </androidx.constraintlayout.widget.ConstraintLayout>
        
            </androidx.cardview.widget.CardView>
        
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        
        <ImageView
            android:layout_width="20dp"
            android:layout_height="30dp"
            android:layout_marginStart="170dp"
            android:layout_marginLeft="170dp"
            android:layout_marginTop="25dp"
            android:src="@drawable/ic_box"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-05-05
          • 2018-02-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-02
          相关资源
          最近更新 更多