我正在尝试创建一个包含卡片 View 列表的应用程序。我在这样的标记中添加了三个卡片 View :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="james_parsons.github.io.fblafashionapp.MainActivity">
<android.support.v7.widget.CardView android:layout_width="350dp"
android:layout_height="350dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="350dp"
android:layout_height="350dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="350dp"
android:layout_height="350dp">
</android.support.v7.widget.CardView>
</RelativeLayout>
然而,这会将所有 CardView
堆叠在一起,并使它们看起来只有一个:
我希望我的卡片像看到的那样垂直堆叠 here .我怎样才能做到这一点?
最佳答案
对于您的情况,LinearLayout
最适合您。此外,由于布局可以超过一个屏幕,您应该使用 ScrollView
将其包裹起来。此外,对于RelativeLayout
,child的默认位置是左上角,如果你想要不同的东西,你必须指定一些布局属性,例如android:layout_below
。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="james_parsons.github.io.fblafashionapp.MainActivity">
<LinearLayout
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.CardView android:layout_width="350dp"
android:layout_height="350dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="350dp"
android:layout_height="350dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView android:layout_width="350dp"
android:layout_height="350dp">
</android.support.v7.widget.CardView>
</LinearLayout>
<ScrollView>
编辑:
另外,不要完全相信布局预览,尽管这次应该是正确的。有几个限制导致布局预览有时不准确,您应该只在快速测试时使用,并且只相信真实设备或模拟器中的布局。
关于android - CardView 都相互堆叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32621037/