Android ListView 卡片行为

标签 android android-layout android-listview android-linearlayout android-cardview

我刚刚注意到一件事。当我在 ListView 中使用布局时,我通常使用 LinearLayout,然后在其中使用 CardView。 CardView 的边距为 10dp,看起来一切正常。但是,当我只使用具有相同属性的 CardView 时,它根本没有给我余地。我做错了什么吗?

布局 1(给我想要的布局):

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        card_view:cardBackgroundColor="@color/white"
        card_view:cardElevation="2sp"
        card_view:cardUseCompatPadding="true">

    </android.support.v7.widget.CardView>

</LinearLayout>

布局 2(无边距):

<android.support.v7.widget.CardView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    card_view:cardElevation="2sp"
    card_view:cardUseCompatPadding="true"
    card_view:cardBackgroundColor="@color/white">

</android.support.v7.widget.CardView>

最佳答案

我也遇到了这个问题。我认为会发生这种情况,因为 ListView 忽略/不支持 margin 属性。

更多相关信息:Why LinearLayout's margin is being ignored if used as ListView row view

旁注:我会在 CardView 周围使用 FrameLayout,它在性能方面更好。

关于Android ListView 卡片行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31167080/

相关文章:

android - 旋转按钮 Espresso 测试点击不工作

android - Android SDK 中的 getWidth/Height() 和 getMeasuredWidth/Height() 有什么区别?

android - 在Android中单击EditText外部时关闭键盘

android - 从 fragment 动态获取 ImageView 中的图像总是返回 0 作为 resourceid

android - 在不缩放内部内容的情况下缩放 Android ListView

java - 在 fragment 中填充 JSON ListView

android - 设备旋转时 TextView 的文本消失

Android Layout Listview在relativelayout下的高度调整

android - 可扩展 ListView 指示器未设置在右侧

android expandablelistview如何设置组项目之间的空间