CardView 去除边框

5

我想要移除我的CardView边框。 我尝试将elevation设置为0dp,将app:cardPreventCornerOverlap设置为false,将app:cardUseCompatPadding设置为true,但我无法从CardView中移除边框。 这是我的xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@android:color/transparent"
    android:orientation="vertical">

    <androidx.cardview.widget.CardView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/IMKAWhite"
        android:layout_gravity="center"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        android:elevation="0dp">

        <RelativeLayout
            android:id="@+id/card"
            android:background="@color/IMKAWhite"
            android:layout_width="150sp"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="150sp"
                android:layout_height="150sp"
                android:id="@+id/profile_photo"
                android:adjustViewBounds="true"
                android:background="@color/colorPrimary"
                android:layout_centerHorizontal="true" />

            <TextView
                android:layout_below="@+id/profile_photo"
                android:id="@+id/profile_name"
                android:layout_marginTop="10sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:textStyle="bold"
                android:textColor="@color/background_tutorial"
                android:textSize="@dimen/text_dp_25"
                android:gravity="center"
                android:text="Medium Text"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/bt_edit_profile"
                android:layout_below="@+id/profile_name"
                android:layout_marginTop="@dimen/text_dp_25"
                android:layout_centerHorizontal="true"
                android:textSize="@dimen/text_dp_16"
                android:padding="5sp"
                android:textColor="@color/background_tutorial"
                android:background="@drawable/ic_bt_edit"
                android:text="@string/edit_profile"
                android:gravity="center" />

        </RelativeLayout>

    </androidx.cardview.widget.CardView>

</LinearLayout>

这是我的屏幕: [![在此输入图片描述][1]][1]
我尝试做这个:
  app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        android:elevation="0dp"

但是我仍然有一个边框


3
将代码中的app:cardElevation="0dp"翻译成中文为:应用程序:cardElevation="0dp" - Flying Dutchman
3个回答

25

使用card_view:cardElevation="0dp"替代android:elevation="0dp"

在根元素中导入命名空间:

xmlns:card_view="http://schemas.android.com/apk/res-auto"

如果您不打算在这个布局中使用高度,并且不想要任何边框,请删除 CardView 元素,将您的视图保留在您的 RelativeLayoutConstraintLayout 中。


1
尝试在您的 CardView/MaterialCardView 上使用 android:outlineProvider="none"
com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_margin="@dimen/_5sdp"
    android:outlineProvider="none"
    app:cardBackgroundColor="@android:color/transparent"
    >

   <!-- Other Views -->

</com.google.android.material.card.MaterialCardView>

0

材料 3

MDC-Android

使用 app:strokeWidth="0dp"

    <com.google.android.material.card.MaterialCardView
        app:strokeWidth="0dp">

Jetpack Compose

看起来默认情况下没有边框。

但是如果你想添加一些,可以使用 Card 的 border 属性:

Card(
    border = CardDefaults.outlinedCardBorder(),
) {
      // ...
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接