我想设计布局时,卡片视图垂直堆叠,卡片之间有 3DP 边距



我想设计垂直堆叠的布局,卡片之间的 3dp 边距,但是当我使用相对视图作为卡片视图的封闭视图时,它没有给出预期的结果,我是 Android 的新手。 此代码片段将帮助您清楚地理解:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="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="com.dpl_it.m.hamzam.widgets.MainActivity">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFF"
card_view:cardElevation="3dp">

<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:gravity="center_horizontal"
android:src="@drawable/bluetooth_connect" />
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txt1"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="16dp"/>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:layout_toEndOf="@+id/img1"
android:layout_toRightOf="@+id/img1"
android:paddingTop="34dp"
android:text="Bluetooth"
android:textSize="24sp"
android:textStyle="bold"
tools:textColor="#3F51B5" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/card_view1"
android:background="#FFFF"
android:elevation="3dp"
android:layout_alignParentBottom="true">
</android.support.v7.widget.CardView>
</RelativeLayout>

使用layout_below将第二个CardView设置在第一个CardView下方对齐:

<android.support.v7.widget.CardView
android:id="@+id/card_view1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/card_view"
android:layout_marginTop="3dp"
card_view:cardBackgroundColor="#ffffff"
card_view:cardElevation="3dp">
</android.support.v7.widget.CardView>

此外,将背景颜色设置为cardBackgroundColor而不是background

最新更新