我想设计卡片视图的布局垂直堆叠,卡片之间有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>
答案 0 :(得分:0)
使用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