【问题标题】:Persist corner radius of a CardView on setting background of a layout inside that CardView在 CardView 内设置布局背景时保持 CardView 的角半径
【发布时间】:2017-07-13 11:58:56
【问题描述】:

我有一个CardView,里面有一些角半径,里面是三个相对布局。当我设置顶部与CardView 顶部对齐的布局背景时,角半径丢失。

如何保持圆角半径?

代码:

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

【问题讨论】:

  • 将每个子相对布局的边距设置为 10dp
  • 你解决了吗?
  • @dharmx 对我来说它在设备上运行良好。只有预览不正确
  • 哦,让我检查一下
  • 是的,你是对的,谢谢。

标签: android android-relativelayout android-cardview cardview


【解决方案1】:

用其他ViewGroup 元素包围您的CardView,例如FrameLayoutLinearLayoutRelativeLayout。即使我遇到了同样的问题,它也以这种方式工作。我在某处读过。

文件: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>

我使用&lt;include&gt; 标签,因为我在不同的地方使用相同的布局。

文件: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>

【讨论】:

  • 我的 CardView 已经在我发布的代码 sn-p 中没有提到的相对布局中。我不明白如何解决问题
  • app:cardPreventCornerOverlap="true"app:cardPreventCornerOverlap="false" 更改
猜你喜欢
  • 2015-06-03
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 1970-01-01
  • 2020-07-21
  • 2019-05-17
  • 1970-01-01
  • 2022-10-17
相关资源
最近更新 更多