android开发之GridLayout详解

在android4.0以上版本中,新增加了GridLayout网格布局,请参考Android 4.0开发之GridLayOut布局实践和浅谈android4.0开发之GridLayout布局

<?xml version="1.0" encoding="utf-8"?> 
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:columnCount="4"//该网格布局有4列
    android:orientation="horizontal" > //水平依次排列,排满4列后,换行排列
    <Button
        android:layout_column="3"//该按钮定位在第4列上(从0开始计算)
        android:text="/" /> 
    <Button android:text="1" /> 
    <Button android:text="2" /> 
    <Button android:text="3" /> 
    <Button android:text="*" /> 
    <Button android:text="4" /> 
    <Button android:text="5" /> 
    <Button android:text="6" /> 
    <Button android:text="-" /> 
    <Button android:text="7" /> 
    <Button android:text="8" /> 
    <Button android:text="9" /> 
    <Button
        android:layout_rowSpan="3"//该按钮在一列上占3行
        android:layout_gravity="fill"//该按钮填充占满一列3行
        android:text="+" /> 
    <Button
        android:layout_columnSpan="2"//该按钮在一行上占2列
        android:layout_gravity="fill"
        android:text="0" /> 
    <Button android:text="00" /> 
    <Button
        android:layout_columnSpan="3"
        android:layout_gravity="fill"
        android:text="=" /> 
</GridLayout>
效果图:

android开发之GridLayout详解_第1张图片

在开发中有些时候完成某些功能,不得不在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);
}


还在继续更新中!!!






你可能感兴趣的:(android,android,布局,4.0,GridLayout,网格)