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

标签 android xml android-layout android-support-library android-cardview

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

enter image description here

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

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

截图如下。

enter image description here

在我的例子中,标题的蓝色背景颜色并没有完全延伸到我的 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/

相关文章:

android - 我的三星 Galaxy 平板电脑中未显示菜单图标

java - 如何在 Fragment 中捕获 onclick 事件

android - 以编程方式设置工具栏 layout_collapseMode

android - 降低 Xamarin.Android + Visual Studio 的调试输出级别

android - 捕获布局屏幕截图后,ImageView 是透明的

c# - 在 C# 中读写 XML

html - 是否有用于将 XML 文件转换为单个 HTML 文件的 Linux 命令行工具序列?

android - 如何在 Android 上获取 CPU 使用率统计信息?

android - 强制折叠 ActionBar 中的选项卡

C# XMLReader - 多个子集合无法通过 XSD 架构进行验证