【问题标题】:Reduce spacing between Views in ListView in Android减少Android中ListView中视图之间的间距
【发布时间】:2021-06-30 08:52:59
【问题描述】:

我是 Android 开发的初学者。 我想减少 ListView 中视图之间的距离。这是我在numberActivity 中的 listView 代码示例:

<ListView 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"
    android:orientation="vertical"
    tools:context="com.example.android.miwok.NumbersActivity"/>

这是我的自定义列表查看代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#78909C"
    android:orientation="horizontal"
    android:padding="8dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageID"
        android:src="@mipmap/ic_launcher"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:orientation="vertical">

        <TextView
            android:id="@+id/miwokText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#65747C"
            android:paddingLeft="8dp"
            android:paddingTop="8dp"
            android:text="TextView"
            android:textColor="#FFFFFF"
            android:textSize="20sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/defaultText"
            android:layout_width="match_parent"
            android:paddingBottom="8dp"
            android:layout_height="wrap_content"
            android:background="#65747C"
            android:paddingLeft="8dp"
            android:text="TextView"
            android:textColor="#E4E495"
            android:textSize="14sp"
            android:textStyle="italic"/>
    </LinearLayout>

</LinearLayout>

看起来像这样:

【问题讨论】:

    标签: java android xml android-listview


    【解决方案1】:

    您不应该在 Main LinearLayout 上定义 android:padding="8dp",因为您要为四个方向(上、下、左和右)提供填充。相反,您可以将它应用到您的左右和底部但不是顶部,这样您可以让您的项目更接近一点。 有些像这样:

        android:paddingBottom ="8dp"
        android:paddingLeft ="8dp"
        android:paddingRight ="8dp"
    

    另外,尝试使用 ma​​rgin insted of padding

    【讨论】: