Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 向listview添加页边距_Android_Listview_Layout - Fatal编程技术网

Android 向listview添加页边距

Android 向listview添加页边距,android,listview,layout,Android,Listview,Layout,我正在学习一个教程,我不明白为什么我应用于相对布局中的列表视图的边距不会影响我在设备上测试它时的外观。但是,如果我将所有内容都放在LinearLayout容器中,它看起来应该是这样的。我不明白为什么没有这种线性布局,布局就不起作用 这是我的listView布局中的代码: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" an

我正在学习一个教程,我不明白为什么我应用于相对布局中的列表视图的边距不会影响我在设备上测试它时的外观。但是,如果我将所有内容都放在LinearLayout容器中,它看起来应该是这样的。我不明白为什么没有这种线性布局,布局就不起作用

这是我的listView布局中的代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
          android:background="@android:color/transparent">
<RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/inbox_horizontal_margin"
    android:layout_marginRight="@dimen/inbox_horizontal_margin"
    android:layout_marginTop="@dimen/inbox_vertical_margin"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageIcon"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/message_list_item_vertical_margin"
        android:paddingBottom="@dimen/message_list_item_vertical_margin"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/content_desc_message_icon"
        android:src="@drawable/ic_action_picture"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/senderLabel"
        android:layout_centerVertical="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_toRightOf="@+id/messageIcon"
        android:layout_toEndOf="@+id/messageIcon"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=" created at: 03.17.15"
        android:id="@+id/createdAtLabel"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/senderLabel"
        android:layout_toEndOf="@+id/senderLabel"/>
</RelativeLayout>
</LinearLayout>

这是代码的结果:

这是没有线性布局的代码:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
    android:layout_marginLeft="@dimen/inbox_horizontal_margin"
    android:layout_marginRight="@dimen/inbox_horizontal_margin"
    android:layout_marginTop="@dimen/inbox_vertical_margin"
    android:background="@android:color/white">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/messageIcon"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/message_list_item_vertical_margin"
        android:paddingBottom="@dimen/message_list_item_vertical_margin"
        android:layout_alignParentStart="true"
        android:contentDescription="@string/content_desc_message_icon"
        android:src="@drawable/ic_action_picture"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/senderLabel"
        android:layout_centerVertical="true"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_toRightOf="@+id/messageIcon"
        android:layout_toEndOf="@+id/messageIcon"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text=" created at: 03.17.15"
        android:id="@+id/createdAtLabel"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/senderLabel"
        android:layout_toEndOf="@+id/senderLabel"/>
</RelativeLayout>

这是这个布局的结果:


页边距在视图之外,ListView默认使用AbsListView.LayoutParams,它不包括任何页边距支持,只包括高度和宽度,这就是为什么它会忽略页边距的参数值。请尝试为相对布局使用填充。

在线性布局中嵌套相对布局也不是必需的。此处为r7v,否则我应用于相对布局的边距根本不起作用,并且列表视图之间没有空格。这是listview行布局吗?我不确定“行”是什么意思,但是这是我的列表视图的唯一布局为什么不使用dividerheight作为行间距,并为listview的父布局提供填充,你能将你的活动/片段布局发布到你有listview的地方吗?我知道,所以将相对布局放在另一个布局中作为一个容器允许我们使用边距,如果我是对的话。谢谢你的回答。