【问题标题】:Change row height in gridview在gridview中更改行高
【发布时间】:2015-05-17 04:06:16
【问题描述】:

我是 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 的布局(我使用了两个,一个用于 wordsearch,一个用于存储搜索词:

<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" />

【问题讨论】:

  • 欢迎 JasperMoneyShot,您能否编辑您的问题并添加您用于 GridView 的布局 xml?这将使人们有机会看到您可能出错的地方并更容易提出建议。
  • 你看过这个documentation吗?
  • CodeMonkey:好的,谢谢。久美穗:是的,我看过它,但不知道如何让它做我想做的事,当我尝试它时,拼图根本没有显示。

标签: android gridview


【解决方案1】:

自己设法弄清楚如何做到这一点,只是把答案贴出来,以防其他人遇到同样的问题。为了修复行高,我编写了一个自定义 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-01-23
    • 1970-01-01
    • 2013-02-18
    相关资源
    最近更新 更多