基本布局-----GridLayout(网格布局)

这篇博客介绍了如何使用GridLayout在Android中构建一个简单的计算器布局。通过将容器划分为6行4列的网格,放置TextView和Button组件,实现了数字和运算符号的布局。每个组件的位置和大小通过GridLayout的spec方法进行指定,按钮的文本、大小、边距等属性也进行了详细设置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将整个容器划分为rows×columns个网格,每个网格可以放一个组件,也可以设置一个组件横跨多少列,纵跨多少行。
实例:计算器

<?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"
    tools:context=".MainActivity"
    android:id="@+id/root">
    <!--定义一个横跨四列的文本框,并设置颜色背景等属性-->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="0"
        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"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
     <!--定义一个横跨四列的按钮-->
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_columnSpan="4"
        android:text="清除"/>
</GridLayout>
package com.example.gridlayoutactivity;

import androidx.appcompat.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=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 colSpec=GridLayout.spec(i%4);
            GridLayout.LayoutParams params=new GridLayout.LayoutParams(rowSpec,colSpec);
            //指定该组件占满父容器
            params.setGravity(Gravity.FILL);
            gridLayout.addView(bn,params);
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值