如何在cardview上添加彩色边框?

新手上路,请多包涵

我是 Android 新手,这是我在这里的第一个问题。

我正在尝试在卡片视图的开头添加彩色垂直边框。如何在 xml 上实现它?我尝试用空的文本视图添加它,但它弄乱了整个卡片视图本身。例如,请检查下面发布的图片链接。

cardview 上的彩色边框

activity_main.xml

 <android.support.v7.widget.CardView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    card_view:contentPadding="16dp"
    card_view:cardElevation="2dp"
    card_view:cardCornerRadius="5dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Headline"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Title" />

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Body1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Content here" />

    </LinearLayout>

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

非常感谢

原文由 George Forster 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.7k
2 个回答

尝试做:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardElevation="2dp"
    card_view:cardCornerRadius="5dp">

    <FrameLayout
        android:background="#FF0000"
        android:layout_width="4dp"
        android:layout_height="match_parent"/>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:orientation="vertical">

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Headline"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Title" />

        <TextView
            style="@style/Base.TextAppearance.AppCompat.Body1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Content here" />

    </LinearLayout>

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

这会从卡片视图中删除填充并添加带有颜色的 FrameLayout。然后,您需要修复 LinearLayout 中的填充,然后修复其他字段

更新

如果要保留卡片角半径,请在可绘制文件夹中创建 card_edge.xml:

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#F00" />
    <size android:width="10dp"/>
    <padding android:bottom="0dp" android:left="0dp" android:right="0dp" android:top="0dp"/>
    <corners android:topLeftRadius="5dp" android:bottomLeftRadius="5dp"
        android:topRightRadius="0.1dp" android:bottomRightRadius="0.1dp"/>
</shape>

并在框架布局中使用 android:background="@drawable/card_edge"

原文由 kandroidj 发布,翻译遵循 CC BY-SA 3.0 许可协议

由于公认的答案要求您添加框架布局,因此您可以在此处使用材料设计来做到这一点。

如果你还没有添加这个

implementation 'com.google.android.material:material:1.0.0'

现在将 Cardview 更改为 MaterialCardView

 <com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="2dp"

app:strokeWidth="1dp"
app:strokeColor="@color/black">

现在您需要将活动主题更改为 Theme.Material。如果您正在使用 Theme.Appcompact,我建议您在未来的项目中使用 Theme.Material,以便在您的应用中拥有更好的材料设计。

     <style name="AppTheme" parent="Theme.MaterialComponents.DayNight">

原文由 Rahul 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题