我会尝试给你一个片段,但老实说,没有必要为你的情况使用 GridView,因为无论如何你的所有项目都在屏幕上。LinearLayout
您可以在一个小循环中创建几个s 来获得结果。
我建议您根据屏幕宽度在运行时设置 columnWidth。
并且当膨胀子视图时,您的适配器应该提供列宽和高度来设置它们。在这种情况下,您需要摆脱 numColumns。请记住,将 numColumns 与 columnWidth 一起使用是没有意义的,尤其是当您想要填充整个空间时。如果要设置 numColumns,请删除 columnWidth。
解决方案:
结果如下:
首先,我们创建布局。就我而言,它是MainActivity
的布局,被称为activity_main.xml
. (请注意没有 GridView,因为我稍后会在代码中添加它):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/header"
android:background="#444"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@android:color/white"
android:text="Dynamic Static GridView" />
<TextView
android:id="@+id/footer"
android:gravity="center"
android:background="#444"
android:textColor="@android:color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Shush" />
</RelativeLayout>
我们的 GridView 元素的布局位于item_grid.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
现在诀窍在 MainActivity 中(我已经注释了一些代码供您理解):
包 com.example.dynamicstaticgridview;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.RelativeLayout;
import android.app.Activity;
import android.graphics.Color;
/**
* @author Sherif elKhatib
*
*/
public class MainActivity extends Activity {
private static String items[]; //these are temporary items :p do not use them
static {
items = new String[7*7];
for(int i=0;i<7*7;i++) {
items[i] = String.valueOf(i);
}
}
int numberOfColumns = 7; //defaulting to 7, you can change it when you know
int numberOfRows = 7; //defaulting to 7, you can change it when you know
GridView mGrid;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.BELOW, R.id.header);
params.addRule(RelativeLayout.ABOVE, R.id.footer);
mGrid = new GridView(this) {
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if(!calculated)
getDimens();
}
};
mGrid.setVerticalSpacing(0);
mGrid.setHorizontalSpacing(0);
mGrid.setStretchMode(GridView.NO_STRETCH);
mGrid.setBackgroundColor(Color.rgb(100, 10, 10));
((RelativeLayout)findViewById(R.id.rootview)).addView(mGrid, params);
}
private int mCellWidth;
private int mCellHeight;
boolean calculated = false;
protected void getDimens() {
calculated = true;
//here you might have some rounding errors
//thats why you see some padding around the GridView
mCellWidth = mGrid.getWidth()/numberOfColumns;
mCellHeight = mGrid.getHeight()/numberOfRows;
mGrid.setColumnWidth(mCellWidth);
mGrid.setNumColumns(numberOfColumns);
mAdapter = new ArrayAdapter<String>(this, R.layout.item_grid, R.id.text, items) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
GridView.LayoutParams params = null;
if(convertView == null) {
params = new GridView.LayoutParams(mCellWidth, mCellHeight);
}
convertView = super.getView(position, convertView, parent);
if(params != null) {
convertView.setLayoutParams(params);
}
return convertView;
}
};
mGrid.setAdapter(mAdapter);
}
}
评论:你最好找到一个合适的算法来选择mCellWidth
, mCellHeight
,numberOfColumns
和numberOfRows
。