CardViews彼此堆积在一起

时间:2015-09-17 00:59:38

标签: android user-interface android-5.0-lollipop material-design

我正在尝试创建一个包含cardview列表的应用。我在标记中添加了三个cardview:

<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堆叠在一起,并使它们只显示为一个:

It only seem like there is one <code>CardView</code>

我希望我的卡片像here那样垂直堆叠。我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:2)

对于你的情况,LinearLayout最适合你。另外,因为布局可能比一个屏幕长,所以应该用ScrollView包装它。此外,对于RelativeLayout,子项的默认位置是左上角,如果您想要不同的东西,则必须具体某些布局属性,例如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>

修改

另外,不要完全信任布局预览,尽管这次应该是正确的。有几个限制使布局预览有时不准确,您应该只在快速测试时使用,并且只信任真实设备或模拟器中的布局。