CardView在CardView的设置背景上保留角点半径

问题描述:

我有一个CardView,其中有一些角落半径,其中有三个相对的布局。当我设置其顶部与CardView顶部对齐的布局的背景时,拐角半径会丢失。CardView在CardView的设置背景上保留角点半径

enter image description here

如何保持圆角半径?

代码:

<android.support.v7.widget.CardView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="false" 
    android:layout_centerHorizontal="false" 
    android:layout_centerInParent="true" 
    app:cardCornerRadius="10dp" 
    android:clipToPadding="true" 
    app:cardPreventCornerOverlap="false"> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rlBase" 
     android:clipChildren="true" 
     android:clipToPadding="true" 
     android:animateLayoutChanges="false"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="30dp" 
      android:id="@+id/rlOverallPerformance" 
      android:background="@color/lightBlue" 
      android:clipToPadding="false" 
      android:clipChildren="false"> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="30dp" 
      android:id="@+id/rlRealizedValue" 
      android:layout_below="@+id/rlOverallPerformance"> 

     </RelativeLayout> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="30dp" 
      android:id="@+id/rlUnrealizedValue" 
      android:layout_below="@+id/rlRealizedValue" 
      android:elevation="1dp"> 

     </RelativeLayout> 

    </RelativeLayout> 

</android.support.v7.widget.CardView> 
+0

设置的空白高达每个孩子相对布局 –

+0

是你解决它的10dp? – dharmx

+1

@dharmx对我来说,它在设备上工作得很好。只有预览不正确 –

环绕你的CardView与像FrameLayoutLinearLayoutRelativeLayout其他ViewGroup元素。即使我面临同样的问题,那么它就是这样工作的。我在某处读过它。

文件:listitem_screen.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <include layout="@layout/listitem_screen_base" /> 
</RelativeLayout> 

我使用<include>标签,因为我在不同的地方使用相同的布局。

文件:listitem_screen_base.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/root" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    app:cardBackgroundColor="@color/windowBackground" 
    app:cardCornerRadius="@dimen/rect_corner_radius" 
    app:cardElevation="@dimen/cardview_default_elevation" 
    app:cardPreventCornerOverlap="true" 
    app:cardUseCompatPadding="true"> 

    <LinearLayout 
     android:id="@+id/ll_base" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"> 

      <com.company.app.custom.CustomImageView 
       android:id="@+id/img_poster" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:adjustViewBounds="true" 
       android:scaleType="fitXY" 
       android:src="@color/black3" 
       app:calculation_type="height" 
       app:ratio_height="@integer/inspire_img_height_ratio" 
       app:ratio_width="@integer/inspire_img_width_ratio" 
       app:riv_corner_radius_top_left="@dimen/rect_corner_radius" 
       app:riv_corner_radius_top_right="@dimen/rect_corner_radius" /> 

      <com.company.app.custom.FontTextView 
       android:id="@+id/lbl_new" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       android:layout_marginBottom="20dp" 
       android:background="@drawable/shape_rect_round_border_right_yellow_fill" 
       android:paddingBottom="2dp" 
       android:paddingEnd="6dp" 
       android:paddingLeft="6dp" 
       android:paddingRight="6dp" 
       android:paddingStart="6dp" 
       android:paddingTop="2dp" 
       android:text="@string/s_new" 
       android:textColor="?android:attr/textColorPrimary" 
       android:textSize="9.72sp" 
       app:customFont="@string/font_circular_book" /> 

     </FrameLayout> 

     <com.company.app.custom.FontTextView 
      android:id="@+id/lbl_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="start|center_vertical" 
      android:layout_marginBottom="13dp" 
      android:layout_marginEnd="10dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginStart="10dp" 
      android:layout_marginTop="15dp" 
      android:ellipsize="end" 
      android:gravity="center_vertical|start" 
      android:maxLines="2" 
      android:minHeight="38dp" 
      android:textAlignment="viewStart" 
      android:textColor="?android:textColorPrimaryInverse" 
      android:textSize="16sp" 
      app:customFont="@string/font_circular_book" /> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="13dp" 
      android:layout_marginEnd="10dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginRight="10dp" 
      android:layout_marginStart="10dp"> 

      <com.company.app.custom.FontTextView 
       android:id="@+id/lbl_time" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:textColor="?android:attr/textColorTertiary" 
       android:textSize="12sp" 
       app:customFont="@string/font_avenir_medium" /> 

      <ImageView 
       android:id="@+id/dot1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginEnd="14dp" 
       android:layout_marginLeft="14dp" 
       android:layout_marginRight="14dp" 
       android:layout_marginStart="14dp" 
       android:layout_toEndOf="@+id/lbl_time" 
       android:layout_toRightOf="@+id/lbl_time" 
       android:scaleType="centerInside" 
       android:src="@drawable/dot" /> 

      <com.company.app.custom.FontTextView 
       android:id="@+id/lbl_views" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toEndOf="@+id/dot1" 
       android:layout_toRightOf="@+id/dot1" 
       android:textColor="?android:attr/textColorTertiary" 
       android:textSize="12sp" 
       app:customFont="@string/font_avenir_medium" /> 

      <ImageView 
       android:id="@+id/dot2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerVertical="true" 
       android:layout_marginEnd="14dp" 
       android:layout_marginLeft="14dp" 
       android:layout_marginRight="14dp" 
       android:layout_marginStart="14dp" 
       android:layout_toEndOf="@+id/lbl_views" 
       android:layout_toRightOf="@+id/lbl_views" 
       android:scaleType="centerInside" 
       android:src="@drawable/dot" /> 

      <com.company.app.custom.FontTextView 
       android:id="@+id/lbl_tag" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_toEndOf="@+id/dot2" 
       android:layout_toRightOf="@+id/dot2" 
       android:textColor="@color/colorAccent" 
       android:textSize="12sp" 
       app:customFont="@string/font_avenir_medium" /> 

     </RelativeLayout> 

    </LinearLayout> 
</android.support.v7.widget.CardView> 
+0

我的CardView已经在我发布的代码片段中没有提到的相对布局中。我不明白怎样才能解决问题 –

+0

从'app:cardPreventCornerOverlap =“false”更改'app:cardPreventCornerOverlap =“true”'' –