android - 在 CardView 内设置布局背景时保留 CardView 的角半径

标签 android android-relativelayout android-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>

最佳答案

包围你的 CardView与其他ViewGroup类似 FrameLayout 的元素, LinearLayoutRelativeLayout .即使我遇到了同样的问题,然后它就这样工作了。我在某个地方读到的。

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

关于android - 在 CardView 内设置布局背景时保留 CardView 的角半径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42408378/

相关文章:

java - 无法获取相机 PutExtra 方法的文件 Uri

安卓 5( Lollipop ): Is it possible to make the Background of a Notification transparent?

android - 打开数字键盘而不强制 EditText 仅为数字

android - LinearLayout 中的RelativeLayout 无法正确包装内容

android - 两个或多个嵌套的 RelativeLayout

android - MaterialCardView 内的 subview 缩小并消失

android - 如何在 BaseAdapter Get View 方法扩展的类中显示 Toast

android - 如果没有记录,ImageView 不显示图像

java - 阴影未显示在 CardView 中

java - RecyclerView 选择列表中的每 8 个项目。怎么能阻止呢?