Android 如何在 Dialog 中适配 listview

Android how to fit listview in Dialog

当 ListView 包含超过 12 个 ListItem 时,我无法将 ListView 放入弹出对话框。是否有任何我在这里遗漏的属性,或者我是否必须在后面的代码中自己计算最大大小?

当一切都完美契合时,我的布局看起来像这样:

当我有太多的项目时,你可以看到我的 LinearLayout 在列表视图和按钮下面消失了。

这是我的 Layout.xml

<?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:background="@drawable/backgroundgradient" android:orientation="vertical">
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/border" android:orientation="vertical">
            <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:orientation="horizontal">
                <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Caption" android:textAppearance="?android:attr/textAppearanceMedium" />
            </RelativeLayout>
            <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                <TextView style="@style/dialog_label" android:text="test" />
                <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
            </LinearLayout>
            <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                <TextView style="@style/dialog_label" android:text="test" />
                <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
            </LinearLayout>
            <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                <TextView style="@style/dialog_label" android:text="test" />
                <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
            </LinearLayout>
            <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                <TextView style="@style/dialog_label" android:text="test" />
                <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
            </LinearLayout>
        </LinearLayout>
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/border" android:orientation="vertical">
            <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
                <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:text="Caption" android:textAppearance="?android:attr/textAppearanceMedium" />
            </RelativeLayout>
            <ListView android:layout_width="match_parent" android:layout_height="match_parent" android:divider="@android:color/transparent" android:dividerHeight="3dp" android:paddingTop="3dp" android:paddingStart="3dp" android:paddingEnd="1dp" android:choiceMode="singleChoice" android:listSelector="@drawable/selector_list_item"></ListView>
        </LinearLayout>
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical">
            <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="3dp" android:background="@drawable/border" android:orientation="vertical">
                <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical">
                    <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="10dp">
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Caption" android:textAppearance="?android:attr/textAppearanceMedium" />
                    </RelativeLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">
                        <TextView style="@style/dialog_label" android:text="test" />
                        <TextView style="@style/dialog_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
        <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="vertical">
            <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_weight="1" android:paddingLeft="2dp" android:paddingRight="2dp" android:text="OK" android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

您可以通过两种方式做到这一点:

第一个:

将所有内容或您想要的内容放入 "ScrollView",无论您放置多少项目,您的项目都会滚动。

例如:

<?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:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

       <!-- PUT EVERYTHING YOU WANT HERE -->

    </ScrollView>
</RelativeLayout>

第二个:

使用适配器和列表项将所有项放入 ListView:

item_list.xml(在 res > layout > New > XML > Layout XML File):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:layout_margin="20dp"
        android:id="@+id/item_text"
        android:text="Test"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

你的列表适配器(在 java > yourPackageName > new > Kotlin File/Class):

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.TextView

class ListAdapter (private val context: Context, private val listItems: ArrayList<String>) : BaseAdapter() {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view = LayoutInflater.from(context).inflate(R.layout.item_list, null)

        val text = view.findViewById<TextView>(R.id.item_text)
        text.text = listItems[position]

        return view
    }

    //It doesn't matter right now
    override fun getItem(position: Int): Any {
        return ""
    }

    //It doesn't matter right now
    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getCount(): Int {
        return listItems.size
    }
}

你的activity_main.xml(其中ListView是:)

<?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:orientation="vertical">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Caption"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="test" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="test" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="test" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="test" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="" />
            </LinearLayout>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="3dp">

            <RelativeLayout
                android:id="@+id/relative"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:text="Caption"
                    android:textAppearance="?android:attr/textAppearanceMedium" />
            </RelativeLayout>

            <ListView
                android:id="@+id/myListView"
                android:layout_below="@id/relative"
                android:layout_above="@id/button"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:choiceMode="singleChoice"
                android:divider="@android:color/transparent"
                android:dividerHeight="3dp"
                android:paddingStart="3dp"
                android:paddingLeft="3dp"
                android:paddingTop="3dp"
                android:paddingBottom="3dp"
                android:paddingEnd="1dp"
                android:paddingRight="1dp"></ListView>

            <Button
                android:id="@+id/button"
                android:layout_alignParentBottom="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:layout_weight="1"
                android:paddingLeft="2dp"
                android:paddingRight="2dp"
                android:text="OK"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </RelativeLayout>

    </LinearLayout>
</RelativeLayout>

MainActivity(调用 ListView 适配器的地方):


import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val itemsList = ArrayList(
            arrayListOf(
                "test", "test", "test", "test", "test", "test", "test", "test", "test", "test",
                "test", "test", "test", "test", "test", "test", "test", "test", "test", "test",
                "test", "test", "test", "test", "test", "test", "test", "test", "test", "test"
            )
        )

        myListView.adapter = ListAdapter(this, itemsList)

    }
}