GridLayout案例

news2025/7/8 1:30:47

GridLayout-网格布局案例

1.网格布局-GridLayout

1.简介

  • 无限细的线绘制的分割区域成行成列,和棋盘的样子差不多

2.注意点

  • 自己设置行数和列数
  • 自己控件在几行几列
  • 自己定义跨越的行数和列数
  • 自己设置子布局的排列的样式

3.常见属性

4.网格布局属性

  • android:columnCount:设置布局管理器的列数,控件会自动换行进行排列;
  • android:rowCount:设置布局管理器的行数
  • android:layout_row:设置控件所在的行
  • android:layout_column:设置控件所在的列
  • android:layout_rowSpan:跨越的行数
  • android:layout_columnSpan:跨越的列数
  • layout_gravity=“fill”:该控件填满所跨越的整行或整列
  • layout_gravity=“fill_horizontal”:该控件填满所跨越的整行

2.计算器案例

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".GridLayoutTest">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="计算器-网格布局"
        android:textSize="30dp"
        />
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:background="#ece7e7"
            android:useDefaultMargins="false"
            >
<!--            第一行:显示结果-->
            <TextView
                android:layout_row="0"
                android:layout_columnSpan="4"
                android:layout_rowWeight="3"
                android:layout_columnWeight="1"
                android:gravity="bottom|right"
                android:text="0"
                android:textSize="15dp"
                />
<!--            第二行-->
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="1"
                android:layout_column="0"
                android:text="AC"
                android:gravity="center"
                android:background="@color/white"
                android:textColor="#f68904"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_margin="1dp"
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="1"
                android:layout_column="1"
                android:text="退格"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"

                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="1"
                android:layout_column="2"
                android:text="/"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="1"
                android:layout_column="3"
                android:text="*"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
<!--            第三行-->
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="2"
                android:layout_column="0"
                android:text="7"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="2"
                android:layout_column="1"
                android:text="8"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="2"
                android:layout_column="2"
                android:text="9"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="2"
                android:layout_column="3"
                android:text="-"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
<!--            第四行-->
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="3"
                android:layout_column="0"
                android:text="4"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="3"
                android:layout_column="1"
                android:text="5"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="3"
                android:layout_column="2"
                android:text="6"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="3"
                android:layout_column="3"
                android:text="+"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
<!--            第五行-->
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="4"
                android:layout_column="0"
                android:text="1"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="4"
                android:layout_column="1"
                android:text="2"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="4"
                android:layout_column="2"
                android:text="3"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="4"
                android:layout_rowSpan="2"
                android:text="="
                android:textColor="#f68904"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
<!--            第六行-->
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="5"
                android:layout_column="0"
                android:text="%"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="5"
                android:layout_column="1"
                android:text="0"
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
            <TextView
                android:layout_columnWeight="1"
                android:layout_rowWeight="1"
                android:layout_row="5"
                android:layout_column="2"
                android:text="."
                android:gravity="center"
                android:background="@color/white"
                android:textSize="15dp"
                android:layout_margin="1dp"
                />
        </GridLayout>

</LinearLayout>

改进版:带简易计算

//网格布局
public class GridLayoutTest extends AppCompatActivity {
    TextView _11,_12,_13,_14,
             _21,_22,_23,_24,
             _44,result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_layout_test);
        initView();



    }

    public void  initView(){
        _11=findViewById(R.id._11);
        _12=findViewById(R.id._12);
        _13=findViewById(R.id._13);
        _14=findViewById(R.id._14);
        _21=findViewById(R.id._21);
        _22=findViewById(R.id._22);
        _23=findViewById(R.id._23);
        _24=findViewById(R.id._24);
        _44=findViewById(R.id._44);
        result=findViewById(R.id.result);
        _14.setClickable(true);
        _21.setClickable(true);
        _22.setClickable(true);
        _44.setClickable(true);
        _21.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result.setText("7");
            }
        });

        _22.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result.setText(result.getText()+"8");
            }
        });
        _14.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result.setText(result.getText()+"*");
            }
        });
        _44.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                result.setText((7*8)+"");
            }
        });
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/109278.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【软件测试】测试人的懊恼,你要揭开的秘密复现bug......

目录&#xff1a;导读前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09;前言 我们测试人常常会懊…

分布式微服务框架SpringCloud Alibaba学习(1)

springCloud 发展历史以及SpringCloud Alibaba概述 一.Why SpringCloud Alibaba? 1.微服务cloud新闻&#xff1a; 2020-12-22日Spring 官方博客宣布&#xff0c;Spring Cloud 2020.0.0正式发布。2020.0.0是第一个使用新的版本号命名方案的Spring Cloud 发行版本。在此之前S…

上次面试跪在了Redis上,刷完腾讯云大神亲码的“redis深度笔记”,终面进腾讯

前言 作为这个时代码代码的秃头人员&#xff0c;对Redis肯定是不陌生的&#xff0c;如果连Redis都没用过&#xff0c;还真不好意思出去面试&#xff0c;指不定被面试官吊打多少次。 毕竟现在互联网公司和一些创业公司都要用到Redis&#xff0c;像亚马逊、谷歌、阿里、腾讯都要使…

哈啰出行高质量故障复盘法:“3+5+3”(附模板)

# 一分钟精华速览 #故障复盘指的是及时把过去发生的错误&#xff0c;最大程度转化为未来可以规避的办法&#xff0c;其核心是不断减少失败因子繁衍的温床&#xff0c;将它们牢牢地掌控在不至于引发危机的范围之中。 作为国民基础设施的哈啰出行&#xff0c;在保障超5.3亿注册用…

按照等分份数或者分割点索引号列表将一个数组拆分为多个数组hsplit()

【小白从小学Python、C、Java】 【计算机等级考试500强双证书】 【Python-数据分析】 按照等分份数或者分割点索引号列表 将一个数组拆分为多个数组 hsplit() [太阳]选择题 以下关于python代码表述有误的一项是? import numpy as np myArraynp.array([[0,1,2,3,4,5],[10,11,1…

“0基础、学历无优势、逻辑能力一般……”能转行做程序员吗?

此前&#xff0c;拉勾数据研究院对程序员群体做了一次深入调查&#xff0c;并发布了《2022程序员群体职场洞察报告》&#xff0c;报告显示&#xff0c;“高薪”依然是程序员的职业标签之一。在调查的程序员群体中&#xff0c;年薪在10-30万元之间的人数占比为66.7%&#xff0c;…

Sharding-JDBC(四)集成dynamic-datasource

目录1.Maven依赖2.yml配置3.DataSourceConfig.java4.TUserService.java5.TUserServiceImpl.java6.测试代码7.测试结果8.源码地址实现原理&#xff1a; 通过 DataSourceConfig.java 将ShardingJDBC数据源配置为动态数据源之一。通过 DS(DataSourceConfig.SHARDING_DATA_SOURCE_…

Coinbase Vntures:Web3社交堆栈指南

概述 Web3社交网络赋予用户对其数据、身份和关系的所有权及可移植性&#xff0c;同时支持无需许可的开发。 Web3社交堆栈有四层&#xff1a;托管、社交原语、profile和应用程序。 例如&#xff1a;Farcaster是一款类似twitter的社交应用程序&#xff0c;它创建在开放的社交图…

HTTPS协议的密钥交换流程

前言 HTTPS 常用的密钥交换算法有两种&#xff0c;分别是 RSA 和 ECDHE 算法。 其中&#xff0c;RSA 是比较传统的密钥交换算法&#xff0c;它不具备前向安全的性质&#xff0c;因此现在已很少服务器使用。而 ECDHE 算法具有前向安全&#xff0c;所以被广泛使用。 注&#xf…

18 | 如何处理k8s证书过期

目录1 证书过期2 常用命令2.1 使用统一命令查看2.2 查看apiserver.crt证书时间2.3 查看secret2.4 查看ingress3 k8s证书过期处理方法1 证书过期 证书在使用的过程中&#xff0c;通常是一年有效期&#xff0c;到期后&#xff0c;需要重新续期。 2 常用命令 2.1 使用统一命令查…

智慧车行预约小程序,汽车保养、维修、美容、检测预测小程序,前后端完整代码包括车行动态,养车常识,保养预约,维修预约,洗车美容预约

功能介绍 智慧车行小程序&#xff0c;是一个专门为洗车/4S/车辆维修行业打造的小程序&#xff0c;前后端完整代码包括车行动态&#xff0c;养车常识&#xff0c;保养预约&#xff0c;维修预约&#xff0c;洗车美容预约&#xff0c;汽车检测预约等功能&#xff0c;采用腾讯提供的…

分布式锁

目录 1. 模拟高并发场景秒杀下单 1.1 导入依赖 1.2 配置application.yml文件 1.3 场景模拟 1.4 案例演示 2. JVM级锁与redis级分布式锁 2.1 JVM级锁 3. redis级分布式锁 3.1 什么是setnx 3.2 场景分析 4. redisson分布式锁 4.1 什么是Redisson 4.2 Redisson工作原…

有关于decoder中的past_key_values参数

我们都知道&#xff0c;encoder-decoder模型在进行generate的时候&#xff0c;会重复地调用decoder &#xff08;i.e., auto-regressive&#xff09;。 也就是&#xff0c;上一个step decoder的预测结果&#xff0c;作为下一个step decoder的输入。 这个时候&#xff0c;由于…

Axios(一) +Promise自定义封装36-42

1. axios 是什么? 1. 前端最流行的 ajax 请求库 2. react/vue 官方都推荐使用 axios 发 ajax 请求 3. 文档: https://github.com/axios/axios 1.2. axios 特点 1. 基于 xhr promise 的异步 ajax 请求库 2. 浏览器端/node 端都可以使用 3. 支持请求&#xff0f;响应拦截器 4…

从零开始,开启属于你的 RTE 漫游之旅!丨漫游指南 x 即将启航

&#x1f914; 什么是「开发者漫游指南」&#xff1f; 「开发者漫游指南」邀请热爱前端开发、关心音视频领域发展、希望进入音视频行业、乐于和大家一起交流成长的小伙伴&#xff0c;通过「开发者漫游指南」与社区共同成长&#xff0c;帮助更多的开发者在实时音视频领域取得进…

Linux中gdb的使用

文章目录gdb的使用方法启动gdb之前的准备工作下载gdb拥有一个带有调试信息的可执行程序正式启动gdb展示源码&#xff08;要先看到源码才知道接下来的步骤……&#xff09;打断点显示所有断点信息运行程序逐过程&#xff08;VS中的F10&#xff09;逐语句&#xff08;VS中的F11&a…

实验四、格子世界(Grid World)

一、实验目的 &#xff08;1&#xff09;熟悉动态规划算法中策略评估过程&#xff1b; &#xff08;2&#xff09;了解如何对问题进行建模处理&#xff0c;包括环境、状态、动作、奖惩值的初始化&#xff1b; 二、实验内容与要求 &#xff08;1&#xff09;掌握动态算法基本…

华为机试_HJ27 查找兄弟单词【中等】

目录 描述 输入描述&#xff1a; 输出描述&#xff1a; 解题过程 提交代码 学习代码 代码一 收藏点 描述 定义一个单词的“兄弟单词”为&#xff1a;交换该单词字母顺序&#xff08;注&#xff1a;可以交换任意次&#xff09;&#xff0c;而不添加、删除、修改原有的字…

Linux系统 PHP安装expect扩展详解

今天继续给大家介绍服务器运维相关知识&#xff0c;本文主要内容是Linux系统 PHP安装expect扩展详解。 一、expect简介 expect是基于tcl语言开发的&#xff0c;用于实现自动和交互式任务进行通信&#xff0c;而无须人的干预。expect是建立在tcl基础上的一个工具&#xff0c;还…

Navicat--对比和同步MySQL表结构的方法

原文网址&#xff1a;Navicat--对比和同步MySQL表结构的方法_IT利刃出鞘的博客-CSDN博客 简介 本文介绍如何使用Navicat对比和同步MySQL表结构的方法。 实际项目中会遇到这样的场景&#xff1a;将测试环境的表结构同步到生产环境。 工具> 结构同步 选择源数据库和目标数据…