GridLayout案例

news/2024/10/25 19:31:59/

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.ppmy.cn/news/5752.html

相关文章

Unprojecting_text_with_ellipses过程分析

文章目录一、单应性1. 图片实例2. 数学表达式二、算法思路1. 算法流程2. 透视失真具体解决方案3. 图片旋转具体解决方案4. 图片文字倾斜具体解决方案三、实际处理过程四、算法问题五、OCR识别原文链接 https://mzucker.github.io/2016/10/11/unprojecting-text-with-ellipses.h…

手办商城系统|Springboot+vue+ElementUI手办商城系统

作者主页&#xff1a;编程指南针 作者简介&#xff1a;Java领域优质创作者、CSDN博客专家 、掘金特邀作者、多年架构师设计经验、腾讯课堂常驻讲师 主要内容&#xff1a;Java项目、毕业设计、简历模板、学习资料、面试题库、技术互助 收藏点赞不迷路 关注作者有好处 文末获取源…

【软件测试】概念篇

目录 一、需求 1.1用户需求 1.2软件需求 1.3需求的重要性 二、测试用例 三、BUG 3.1什么是BUG 3.2如何描述一个BUG 4.3BUG优先级 四、软件开发模型 4.1软件生命周期 4.2开发模型 定义&#xff1a;软件测试就是一系列活动&#xff0c;这些活动是为了评估一个程序或者…

git push踩坑记录【看注意事项】

记录一次git push的踩坑过程&#xff08;详细在注意事项里&#xff0c;列出了具体的解决办法&#xff09;。 push远程仓库命令 使用命令 git init git add . git commit -m "提交说明写在这里" git remote add origin gitgithub.com:xxx/surgical-robot.git git p…

为什么硬盘在macbook上无法编辑?mac不能往移动硬盘拷东西

为什么硬盘在macbook上无法编辑&#xff1f;如果您只想在Mac上查看NTFS文件&#xff0c;只需将NTFS 外置存储设备连接到mac电脑并查看文件。但要编辑或传输文件&#xff0c;则需要NTFS工具。 NTFS文件格式与Mac不兼容&#xff0c;但许多用户仍然喜欢使用NTFS文件&#xff0c;而…

基于松鼠算法改进的DELM预测-附代码

松鼠算法改进的深度极限学习机DELM的回归预测 文章目录松鼠算法改进的深度极限学习机DELM的回归预测1.ELM原理2.深度极限学习机&#xff08;DELM&#xff09;原理3.松鼠算法4.松鼠算法改进DELM5.实验结果6.参考文献7.Matlab代码1.ELM原理 ELM基础原理请参考&#xff1a;https:…

GitHub典藏版,腾讯T14级高级程序员亲码的分布式数据库实践,再次爆火

数据库就是要做好五件事&#xff0c;存储、事务、查询、复制和其他。而对分布式数据库来说&#xff0c;不仅要继续做这五件事&#xff0c;还要多出一件事&#xff0c;分片。在这六件事中&#xff0c;存储和其他这两件事与单体数据库差不多&#xff0c;难点就在事务、查询、复制…

P1220 关路灯(区间dp)

题目描述 某一村庄在一条路线上安装了 nn 盏路灯&#xff0c;每盏灯的功率有大有小&#xff08;即同一段时间内消耗的电量有多有少&#xff09;。老张就住在这条路中间某一路灯旁&#xff0c;他有一项工作就是每天早上天亮时一盏一盏地关掉这些路灯。 为了给村里节省电费&…