android - CardView 都相互堆叠

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

我正在尝试创建一个包含卡片 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 堆叠在一起,并使它们看起来只有一个:

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

我希望我的卡片像看到的那样垂直堆叠 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/

相关文章:

android - 连接SDK求跳转到0 :00

android - 我的生日快乐应用程序在我的手机上强制关闭

android - 找不到 com.android.tools.build :gradle:4. 1.1

java - Android:使用从 InstrumentationTestCase 中的 UiAutomation.executeShellCommand() 返回的 ParcelFileDescriptor

android - RecyclerView 在打开软键盘时隐藏操作栏

android - 为什么我不能在默认命名空间中定义 android 属性?

javascript - 如果我想做 Javascript、AJAX 的工作,我是否需要服务器端知识(例如 Django、Rails)?

java - 无法获得要在面板中显示的 paintComponent() 结果

user-interface - "undefined reference to ` gtk_label_set_xalign '"编译 Vala 应用程序时

android - 禁用汉堡包到箭头动画