微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Android]笔记8-GridLayout布局

GridLayout
概述
  GridLayout即网格布局,它是Android4.0新增的布局管理器,因此需要在Android4.0之后的版本中才能使用该布局。
  GridLayout的作用类似于HTML中的table标签,它把整个容器划分成rowsxcolumns个网格,每个网格可以放置一个组件。
  初次之外,也可以设置一个组件横跨多少列、一个组件纵跨多少行。
  GridLayout提供了setRowCount(int)和setColumnCount(int)方法来控制该网格的行数量和列数量
  为了控制GridLayout布局容器中各子组件的布局分布,GridLayout提供了一个内部类:
GridLayout.layoutParams
XML属性方法
  
XML属性 相关方法 说明
android:alignmentMode setAlignmentMode(int) 设置该布局管理器采用的对齐模式
android:columnCount setColumnCount(int) 设置该网格的列数量
android:columnorderPreserved setColumnorderPreserved(boolean) 设置该网格容器是否保留列序号
android:rowCount setRowCount(int) 设置该网格的行数量
android:rowOrderPreserved setRowOrderPreserved(boolean) 设置该网格容器是否保留行序号
android:useDefaultMargins setUseDefaultMargins(boolean) 设置该布局管理器是否使用认的页边距
设计计算器界面:

<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:rowCount="6"
    android:columnCount="4"
    android:id="@+id/root"
    tools:context="com.dezai.gridlayouttest.MainActivity">
    <!--定义一个横跨4列的文本框,
    并设置该文本框的前景色、背景色等属性-->
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:textSize="50sp"
        android:layout_marginLeft="2pt"
        android:layout_marginRight="2pt"
        android:padding="3pt"
        android:layout_gravity="right"
        android:background="#eee"
        android:textColor="#000"
        android:text="0"
        />
    <!--定义一个横跨4列的按钮-->
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:layout_gravity="fill"
        android:text="清除"
        />
</GridLayout>
package com.dezai.gridlayouttest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;

public class MainActivity extends AppCompatActivity {
    GridLayout gridLayout;
    //定义16个按钮的文本
    String[] chars=new String[]{
            "7","8","9","÷","4","5","6","×","1","2","3","-",".","0","=","+",};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        gridLayout =(GridLayout) findViewById(R.id.root);
        for(int i=0;i<chars.length;i++)
        {
            Button bn=new Button(this);
            bn.setText(chars[i]);
            //设置按钮的字体大小
            bn.setTextSize(40);
            //设置按钮四周的空白区域
            bn.setPadding(5,35,5,35);
            //指定该组件所在的行
            GridLayout.Spec rowSpec=GridLayout.spec(i / 4+ 2);
            //指定该组件所在的列
            GridLayout.Spec columnSpec=GridLayout.spec(i%4);

            GridLayout.LayoutParams params=new GridLayout.LayoutParams(rowSpec,columnSpec);

            params.setGravity(Gravity.FILL);
            gridLayout.addView(bn,params);

        }

    }
}

这里写图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐