1

我在网上找不到任何专门提出这个问题的问题

方形边框截图

我将如何在图像中实现红色“第二”边框但在一侧?我正在使用 MaterialCardView 作为主要的外部边框。我是否可以在左侧制作一个带有红色边框的自定义形状并将其用作 MaterialCardView 的背景?

4

2 回答 2

1

这可以MaterialCardViews通过仅更改每张卡片的app:cardBackgroundColorapp:cardCornerRadiusandroid:layout_margin属性来轻松实现三个嵌套。

下面是 Xml 布局示例:

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_margin="10dp"
    app:cardBackgroundColor="#81afdc"
    app:cardCornerRadius="10dp">

    <com.google.android.material.card.MaterialCardView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp"
        app:cardBackgroundColor="#a70e09"
        app:cardCornerRadius="10dp">

        <com.google.android.material.card.MaterialCardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginStart="6dp"
            app:cardBackgroundColor="#ffffff"
            app:cardCornerRadius="6dp">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:text="Sample Text"/>

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

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

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

结果:

material_cardview

于 2021-10-06T20:32:56.810 回答
0

首先,创建两个可绘制资源文件,如下所示:

bg_cardView_inner_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid android:color="@android:color/holo_red_dark"/>

<corners android:radius="7dp"/>

</shape>

bg_cardView_outline_color.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">


<solid android:color="@color/white" />
<stroke android:color="@android:color/holo_blue_light"
    android:width="5dp"/>
<corners android:radius="10dp"/>
</shape>

然后,在您的 XML 文件中,添加这些文件,如下所示:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".DemoActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_marginStart="10dp"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="10dp"
    android:background="@drawable/bg_cardview_outline_color"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        app:cardCornerRadius="5dp"
        app:cardElevation="2dp"
        app:cardUseCompatPadding="true"
        android:paddingBottom="5dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:background="@drawable/bg_cardview_inner_color">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/white"
                android:gravity="center_vertical|center"
                android:layout_marginStart="10dp">
                <ImageView
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:src="@drawable/your_image"
                    app:tint="@color/black"
                    />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="your text"
                    android:textSize="30sp"
                    android:gravity="center"
                    android:textStyle="bold"
                    />
            </LinearLayout>

        </LinearLayout>
    </androidx.cardview.widget.CardView>

</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
于 2021-10-05T02:26:20.257 回答