【问题标题】:How to add colored border on cardview?如何在cardview上添加彩色边框?
【发布时间】:2016-05-24 00:30:32
【问题描述】:

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

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

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>

非常感谢

【问题讨论】:

  • 这里需要使用cardview吗?仅平面线性布局并在您的回收站视图中添加 ItemDecoration 怎么样?
  • 我是 Android 的初学者,recyclerview 对于我目前的技能集来说有点先进。所以,我在这里使用cardview。

标签: android xml android-cardview


【解决方案1】:

从材料设计更新开始,它支持app:strokeColorapp:strokeWidthsee more

使用材料设计更新。将以下代码添加到build.gradle(:app)

dependencies {
    // ...
    implementation 'com.google.android.material:material:1.0.0'
    // ...
  }

并将CardView 更改为MaterialCardView

【讨论】:

  • 找不到 strokeColor。
  • @PrimožKralj 将 cardView 更改为 MaterialCardView
  • CardView更改为MaterialCardView
  • MaterialCardView的全名是com.google.android.material.card.MaterialCardView
  • 引起:java.lang.IllegalArgumentException:此组件上的样式要求您的应用主题为 Theme.MaterialComponents(或后代)。
【解决方案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 中的填充,然后修复其他字段

更新

如果要保留卡片圆角半径,请在 drawable 文件夹中创建 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"

【讨论】:

  • @GeorgeForster 更新为使用可绘制背景来保留卡片角(如果您想保留它们)
  • 对我不起作用。为什么框架布局的宽度为 4 dp?
  • 此答案不应再被标记为正确答案。正确的一个是 Aung Myo Lwin 的那个。
【解决方案3】:

我认为这种解决方案可能效率不高,但它可以达到目的并增加了边框宽度的灵活性。

 <android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="40dp"
    android:layout_gravity="center"
    card_view:cardBackgroundColor="@color/some_color"
    card_view:cardCornerRadius="20dp"
    card_view:contentPadding="5dp"> <!-- Change it to customize the border width -->

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        card_view:cardCornerRadius="20dp"
        card_view:contentPadding="5dp">

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

         <!-- Add your UI elements -->

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

【讨论】:

  • 这是一个糟糕的解决方案。您正在添加视图层次结构级别,这会降低性能。
  • 好主意应该有可用的内部功能
  • 这不是性能最高的,但是我使用它并没有大受欢迎
【解决方案4】:

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

如果你还没有添加这个

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

【讨论】:

    【解决方案5】:

    CardView 扩展了 FrameLayout,因此它支持foreground 属性。使用foreground属性也可以轻松添加边框。

    布局如下:

    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/link_card"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:foreground="@drawable/bg_roundrect_ripple_light_border"
        app:cardCornerRadius="23dp"
        app:cardElevation="0dp">
    </androidx.cardview.widget.CardView>
    
    

    bg_roundrect_ripple_light_border.xml

    <?xml version="1.0" encoding="utf-8"?>
    <ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="@color/ripple_color_light">
    
        <item>
            <shape android:shape="rectangle">
                <stroke
                    android:width="0.5dp"
                    android:color="#DDDDDD" />
                <corners android:radius="23dp" />
            </shape>
        </item>
    
        <item android:id="@android:id/mask">
            <shape android:shape="rectangle">
                <corners android:radius="23dp" />
                <solid android:color="@color/background" />
            </shape>
        </item>
    
    </ripple>
    

    【讨论】:

      【解决方案6】:

      我的解决方案:

      创建一个文件card_view_border.xml

      <?xml version="1.0" encoding="utf-8"?>
      <shape xmlns:android="http://schemas.android.com/apk/res/android">
      <solid android:color="@color/white_background"/>
      <stroke android:width="2dp" 
          android:color="@color/red" />
      <corners android:radius="20dip"/>
      </shape>
      

      并以编程方式设置

      cardView.setBackgroundResource(R.drawable.card_view_border);
      

      【讨论】:

        【解决方案7】:

        我想改进 Amit 提出的解决方案。我正在使用给定的资源而没有添加额外的shapesViews。我给CardView 一个背景色,然后是嵌套布局,白色叠印还有一些leftMargin...

        <?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:cardBackgroundColor="@color/some_color"
            card_view:cardCornerRadius="5dp">
        
        
            <!-- The left margin decides the width of the border -->
        
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:padding="16dp"
                android:layout_marginLeft="5dp"
                android:background="#fff"
                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>
        

        【讨论】:

        • 这是一个优雅的解决方案!谢谢。
        • fill_parent 在很久以前的 android sdk 8 中已被弃用
        【解决方案8】:

        我通过将两个 CardViews 放在一个 RelativeLayout 中解决了这个问题。一个带有边框颜色的背景,另一个带有图像。 (或任何你想使用的)

        注意添加到顶部并开始第二个 CardView 的边距。就我而言,我决定使用 2dp 粗边框。

                    <android.support.v7.widget.CardView
                    android:id="@+id/user_thumb_rounded_background"
                    android:layout_width="36dp"
                    android:layout_height="36dp"
                    app:cardCornerRadius="18dp"
                    android:layout_marginEnd="6dp">
        
                    <ImageView
                        android:id="@+id/user_thumb_background"
                        android:background="@color/colorPrimaryDark"
                        android:layout_width="36dp"
                        android:layout_height="36dp" />
        
                </android.support.v7.widget.CardView>
        
                <android.support.v7.widget.CardView
                    android:id="@+id/user_thumb_rounded"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    app:cardCornerRadius="16dp"
                    android:layout_marginTop="2dp"
                    android:layout_marginStart="2dp"
                    android:layout_marginEnd="6dp">
        
                <ImageView
                    android:id="@+id/user_thumb"
                    android:src="@drawable/default_profile"
                    android:layout_width="32dp"
                    android:layout_height="32dp" />
        
                </android.support.v7.widget.CardView>
        

        【讨论】:

          猜你喜欢
          • 2016-08-03
          • 2018-06-02
          • 1970-01-01
          • 2020-01-26
          • 2020-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-09-24
          相关资源
          最近更新 更多