GridLayout网络布局,类似html里的table,可以设置行数、列数、跨行单元格
GridLayout XML属性
属性 | 方法 | 说明 |
android:alignmentMode |
setAlignmentMode GridLayout.ALIGN_BOUNDS GridLayout.ALIGN_MARGINS |
没效果 设置该布局的对其方式 alignBounds或alignMargins |
android:columnCount | setColumnCount | 列数量 |
android:columnOrderPreserved | setColumnOrderPreserved |
没效果 是否保留列序列号 |
android:rowCount | setRowCount | 行数量 |
android:rowOrderPreserved | setRowOrderPreserved |
没效果 是否保留行序列号 |
android:useDefaultMargins | setUseDefaultMargins |
没效果 是否使用默认的页边距 |
GridLayout 的 LayoutParams
属性 | 方法 | 说明 |
android:layout_column | 在第几列 | |
android:layout_columnSpan | 横跨几列 | |
android:layout_row | 在第几行 | |
android:layout_rowSpan | 横跨几行 | |
android:layout_gravity | 对其方式 |
计算机例子
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <GridLayout android:id="@+id/root" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="4" android:rowCount="6" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_columnSpan="4" android:layout_gravity="fill" > </EditText> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_columnSpan="4" android:text="清除" /> </GridLayout> </LinearLayout>
MainActivity.java
package com.example.dddd; import android.app.Activity; import android.graphics.Point; import android.os.Bundle; import android.view.Gravity; import android.widget.Button; import android.widget.GridLayout; public class MainActivity extends Activity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); GridLayout gl=(GridLayout)findViewById(R.id.root); String[] aa=new String[]{ "1","2","3","+", "4","5","6","-", "7","8","9","*", ".","0","=","/"}; Point size = new Point(); getWindowManager().getDefaultDisplay().getSize(size); for(int i=0;i<aa.length;i++){ GridLayout.Spec column=GridLayout.spec(i%4); GridLayout.Spec row=GridLayout.spec(i/4+2); GridLayout.LayoutParams params=new GridLayout.LayoutParams(row, column); //params.setGravity(Gravity.FILL); Button btn=new Button(this); btn.setText(aa[i]); btn.setWidth((int) (size.x * 0.25)); btn.setHeight((int) (size.x * 0.25)); gl.addView(btn,params); } } }