ListView中的项目宽度

最后发布: 2019-10-17 12:58:36


问题

作为标题,我想在DialogFragment中创建一个列表视图。 这是我的代码:

class WeatherFragment : DialogFragment () {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = inflater.inflate(R.layout.fraglayout, container)
        val str = arrayOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m")
        val myListView = rootView.findViewById(R.id.mc_weather_list) as ListView
        myListView.adapter = ArrayAdapter<String>(requireActivity(), android.R.layout.simple_list_item_1, str)
        myListView.setOnItemClickListener {
            adapterView,
            view,
            position,
            l
            -> Toast.makeText(activity, str[position], Toast.LENGTH_SHORT).show()
        }

        return rootView
    }

}

这样,它就可以完美地像这样Screenshot_1

但是,如果我在列表中添加更多项目,例如

val str = arrayOf("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n")

listview中的项目变得很小Screenshot_2

谁能帮我解决这个问题? 提前致谢!

java android kotlin