更改gridview中的行高

Change row height in gridview

我是 Android 开发的新手,正在制作我的第一个应用程序,它是一个项目的单词搜索应用程序,我正在使用 GridView 显示搜索的字母。我能够使用布局 xml 更改网格中单元格的宽度,但是我无法更改高度,目前它太多了。

我尝试 Google 回答有关如何执行此操作的问题,但我发现的所有内容都是关于在自定义 arrayAdapter 中覆盖 getView 的,但我并没有真正遵循如何执行此操作或如何将高度设置为做的时候固定数量。我在布局文件中将行宽设置为 25dp,并想更改高度以匹配它,但我似乎找不到任何易于遵循的在线方法。

如有帮助,提前打招呼

编辑:这是我填充网格的地方:

//fill GridView with the puzzle input array from the puzzle class
ArrayAdapter<String> gridAdapter = new ArrayAdapter<String>(c, R.layout.cell_layout, todaysPuzzle.puzzleInputArray);
wordsearchGrid.setAdapter(gridAdapter);

//fill ListView with the searchWords array from the puzzle class
ArrayAdapter<String> wordsAdapter = new ArrayAdapter<String>(c, R.layout.cell_layout, todaysPuzzle.searchWords);
wordsList.setAdapter(wordsAdapter);

这是 gridView 的布局(我使用了两个,一个用于单词搜索,一个用于存储搜索词:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/black"
tools:context="little.keith.wordsearch.TodaysActivity" >

<GridView
    android:id="@+id/wordsearchGrid"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_margin="4dp"
    android:background="@android:color/black"
    android:columnWidth="25dp"
    android:gravity="top"
    android:horizontalSpacing="2dp"
    android:numColumns="12"
    android:stretchMode="none"
    android:verticalSpacing="2dp" >
</GridView>

<GridView
    android:id="@+id/wordsList"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="4dp"
    android:background="@android:color/black"
    android:numColumns="auto_fit"
    android:layout_below="@+id/wordsearchGrid"
    android:horizontalSpacing="2dp"
    android:verticalSpacing="2dp"
    android:layout_centerHorizontal="true" >
</GridView>

这里是 cell_layout.xml:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingStart="1dp"
android:paddingEnd="1dp"
android:textAppearance="?android:attr/textAppearanceLarge" />

设法自己弄清楚如何做到这一点,只是把答案贴出来,以防其他人遇到同样的问题。为了修复行高,我编写了一个自定义 ArrayAdapter,它允许我使用 setLayoutParams:

定义单元格尺寸
public class GridAdapter extends ArrayAdapter
{
    Context context;
    Object[] items;
    int resource;

    LayoutInflater inflater;

    public GridAdapter(Context context, int resource, Object[] items) 
    {
        super(context, resource, items);
        this.context = context;
        this.resource = resource;
        this.items = items;

        inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() 
    {
        return items.length;
    }

    @Override
    public Object getItem(int position) 
    {
        return items[position];
    }

    @Override
    public long getItemId(int position) 
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        TextView cell;

        int cellWidth = 40;
        int cellHeight = 50;


        if (convertView == null) 
        {
            // If convertView is null then inflate the appropriate layout file
            convertView = inflater.inflate(resource, null);
        }

        cell = (TextView) convertView.findViewById(R.id.gridCell);

        cell.setText((CharSequence) items[position]);

        // Set height and width constraints for the image view
        cell.setLayoutParams(new LinearLayout.LayoutParams(cellWidth, cellHeight));

        // Set Padding for images
        cell.setPadding(1, 1, 1, 1);

        return convertView;
    }
}

然后在Activity中调用它:

//fill GridView with the puzzle input array from the puzzle class
GridAdapter gridAdapter = new GridAdapter(c, R.layout.cell_layout, todaysPuzzle.puzzleInputArray);
wordsearchGrid.setAdapter(gridAdapter);