可以在layout布局文件中,通过GridView设置显示列数来实现。
同样通过Adapter来为GridView提数据。
<LinearLayout 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"
android:background="@drawable/background"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#ffffff" >
</LinearLayout>
<GridView
android:id="@+id/grid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:numColumns="6" >
</GridView>
</LinearLayout>
通过程序来为GridView加入数据。
这里就直接建立自定义的Adapter,来完成界面的显示。
完成某一格的布局
<LinearLayout 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"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp" >
<TextView
android:id="@+id/stage_num"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:textSize="10sp"
android:gravity="center"
android:textColor="#cccccc" />
</LinearLayout>
建立Adapter类
public class StageAdapter extends BaseAdapter {
private Context ctx;
private List<Map<String, Object>> allValues;
public StageAdapter(Context ctx, List<Map<String, Object>> allValues) {
this.ctx = ctx;
this.allValues = allValues;
}
@Override
public int getCount() {
return allValues.size();
}
@Override
public Object getItem(int position) {
return allValues.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(ctx).inflate(R.layout.stage_line,
null);
// 设置高度
convertView.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT, Globals.SCREEN_HEIGHT / 9));
}
Map<String, Object> map = allValues.get(position);
TextView text = (TextView) convertView.findViewById(R.id.stage_num);
text.getLayoutParams().height = Globals.SCREEN_HEIGHT / 9 - 20;
text.setText(map.get("stageNum").toString());
boolean overFlag = (Boolean) map.get("overFlag");
if (overFlag) {
text.setBackgroundColor(Color.WHITE);
} else {
text.setBackgroundColor(Color.BLACK);
}
return convertView;
}
}
建立Activity
public class StageSelectActivity extends Activity {
private GridView grid;
private List<Map<String, Object>> allValues = new ArrayList<Map<String, Object>>();
private StageAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Globals.init(this);
setContentView(R.layout.activity_stage_select);
grid = (GridView) findViewById(R.id.grid);
// 初始化一些数据
for (int i = 1; i <= 50; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("stageNum", i);
if (i < 10) {
map.put("overFlag", true);
} else {
map.put("overFlag", false);
}
allValues.add(map);
}
adapter = new StageAdapter(this, allValues);
grid.setAdapter(adapter);
// 如果想加入监听,也需要使用onItemClickListener
grid.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Map<String, Object> map = allValues.get(arg2);
if ((Boolean) map.get("overFlag")) {
Toast.makeText(StageSelectActivity.this,
"该关卡可以选择: " + map.get("stageNum"),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(StageSelectActivity.this,
"该关卡不可以选择: " + map.get("stageNum"),
Toast.LENGTH_SHORT).show();
}
}
});
}
}