首页 > 解决方案 > 如何在 Android 的 GridView 中设置子文本框的大小

问题描述

我的 Android 应用程序中有一个 GridView。我注意到,无论我如何指定各个 TextField 的高度/宽度,它们都不会反映在 GridView 的 TextField 中。

现在我正在寻找替代方法$android:columnWidth="90dp"来创建特定宽度的 TextView。但是,如何在 GridView 中设置 TextViews 的高度?

<GridView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/park_cont"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnWidth="90dp"
    android:horizontalSpacing="5dp"
    android:gravity="center"
    android:numColumns="auto_fit"
    android:padding="10dp"
    android:stretchMode="columnWidth"
    android:verticalSpacing="5dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".ParkingView"
    tools:showIn="@layout/activity_parking_view" />

标签: androidandroid-studioandroid-layoutkotlinandroid-gridview

解决方案


您需要覆盖 的getView()ArrayAdapter设置 的LayoutParam高度View。你需要按照以下几行做一些事情,

val adapter = object : ArrayAdapter<String>(
        mContext,
        android.R.layout.simple_list_item_1, flowers
) {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        val tvCell = super.getView(position, convertView, parent)

        //Set the height of the view by settings it's layoutParams.height
        tvCell.layoutParams.height = 1100

        // Return the modified item
        return tvCell
    }
}

mGridView.adapter = adapter

推荐阅读