我是Android开发新手并制作我的第一个应用程序,它是一个项目的wordsearch应用程序,我使用GridView显示搜索字母。我能够使用布局xml改变网格中单元格的宽度,但是我不能改变高度,而且目前它太多了。
我尝试了谷歌如何做到这一点的答案,但我发现所有关于在自定义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" />
答案 0 :(得分: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;
}
}
然后在活动中调用它:
//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);