zoukankan      html  css  js  c++  java
  • Android笔记(十一) Android中的布局——网格布局

             网格布局是Android4.0新增的布局管理器,因此需要在Android4.0之后的版本才可以使用,之前的平台使用该布局的话,需要导入相应的支持库。

             GridLayout的作用类似于HTML中的table标签,它把整个容器划分成row*column个网格,每个网格都可以放置一个组件,也可以设置一个组件横跨多少列、多少行。

             GridLayout提供了setRowCount(int)和setColumnCount(int)方法来控制该网格的行数和列数。

             简单代码示例:

             gridlayout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:rowCount="6"
        android:columnCount="4"
        android:id="@+id/root">
    
        <!-- 定一个一个横跨四列的文本框,并设置该文本框的前景色、背景色等属性 -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="4"
            android:textSize="50sp"
            android:layout_marginLeft="4px"
            android:layout_marginRight="4px"
            android:padding="5px"
            android:layout_gravity="right"
            android:background="#eee"
            android:textColor="#000"
            android:text="0"
            />
        <!-- 定义一个横跨四列的按钮 -->
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_columnSpan="4"
            android:text="清除"/>
    
        <!-- 添加其他按钮 -->
        <Button
            android:text="7"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="8"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="9"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="/"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="4"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="5"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="6"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="*"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="1"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="2"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="3"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="-"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="0"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="."
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="+"
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
        <Button
            android:text="="
            android:layout_rowWeight="1"
            android:layout_columnWeight="1"/>
    
    </GridLayout>

      运行结果:

  • 相关阅读:
    压缩命令
    u盘挂载
    三种不同的空格
    打出圆圈数字①的快捷方法
    循环使用的一个坑
    Python&R:警告信息管理
    Matlab的基本矩阵运算
    R语言-程序执行时间
    Python:n个点的费马问题
    Python网络数据采集(1):博客访问量统计
  • 原文地址:https://www.cnblogs.com/xs104/p/4727461.html
Copyright © 2011-2022 走看看