草庐IT

安卓支持-v7 CardView : Header textview with other background color does not extend until the edges

coder 2023-12-02 原文

我想创建一个带有 CardView 的布局,它包含一个带有其他背景颜色的标题,类似于当前 Google Now 屏幕中显示的内容(见下文)。

颜色较深的标题(包含文本“iPhone 6”)完全延伸到 CardView 的边缘和圆角。

我尝试使用 support-v7 库 (android.support.v7.widget.CardView) 构建一个类似的 CardView。

截图如下。

在我的例子中,标题的蓝色背景颜色并没有完全延伸到我的 CardView 的边缘。在左、上和右边缘可见一个小的白色边框(CardView 的背景颜色)。这似乎是 CardView 圆角的结果。

如何让标题(TextView)的背景颜色完全延伸到 CardView 的边缘和圆角?

这是我的 xml 布局文件的相关部分:

<android.support.v7.widget.CardView
        android:id="@+id/calendar_detail_time_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/calendar_detail_title_card"
        android:layout_margin="6dp"
        card_view:cardCornerRadius="3dp"
        card_view:cardUseCompatPadding="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:background="@color/primaryColor"
                android:padding="8dp"
                android:textSize="@dimen/calendar_detail_large"
                android:textStyle="bold"
                android:id="@+id/header_when"
                android:text="@string/header_when"
                tools:text="When?"/>

            <ImageButton
                android:id="@+id/calendar_detail_add_to_calendar_button"
                style="?android:borderlessButtonStyle"
                android:layout_width="@dimen/calendar_detail_add_to_calendar_image"
                android:layout_height="@dimen/calendar_detail_add_to_calendar_image"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:src="@drawable/ic_calendar_red" />

            <TextView
                android:id="@+id/calendar_detail_time"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@id/header_when"
                android:padding="8dp"
                tools:text="Time" />

        </RelativeLayout>

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

最佳答案

在 xml 中将此代码添加到您的卡中:

card_view:cardPreventCornerOverlap="false"

现在它会一直延伸。

关于安卓支持-v7 CardView : Header textview with other background color does not extend until the edges,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28509784/

有关安卓支持-v7 CardView : Header textview with other background color does not extend until the edges的更多相关文章

随机推荐