在android4.0以上版本中,新增加了GridLayout网格布局,请参考Android 4.0开发之GridLayOut布局实践和浅谈android4.0开发之GridLayout布局
在开发中有些时候完成某些功能,不得不在java代码中实现GridLayout
java代码:
GridLayout.LayoutParams gllpTv;
gllpTv = new GridLayout.LayoutParams();
gllpTv.columnSpec = GridLayout.spec(0, 5);//相当于android:layout_columnSpan="5" spec(start,size)参数为起始位置,占几列
TextView timeText = new TextView(this);
timeText.setText("********");
timeText.setTextSize(26);
timeText.setPadding(10, 50, 0, 5);
gridlayout.addView(timeText, gllpTv);//把控件和布局参数添加到GridLayout
类似Gridview的使用
for (int j = 0; j < playRecordList.size(); j++) {
View v = inflater.inflate(R.layout.gridview_playrecord_item_layout, null);
ImageView playrecord_img = (ImageView) v.findViewById(R.id.play_record_img);
TextView playrecord_name = (TextView) v.findViewById(R.id.play_record_name);
fb.display(playrecord_img, playRecordList.get(j).getVideoImgUrl());
playrecord_name.setText(playRecordList.get(j).getVideoName());
playrecord_img.setFocusable(true);
playrecord_img.setClickable(true);
playrecord_img.setTag(playRecordList.get(j));
playrecord_img.setOnClickListener(myCliclListener);
gridlayout.addView(v);
}