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>

      运行结果:

  • 相关阅读:
    监听手机晃动(摇一摇)SensorEventListener
    adb logcat 命令行用法
    设计模式:观察者模式
    设计模式学习笔记-观察者模式
    Android中Parcelable序列化总结
    2048 Puzzle游戏攻略
    projecteuler----&gt;problem=14----Longest Collatz sequence
    桥模式设计模式进入Bridge
    SessionA和pplication网上聊天室的网络范例
    [ACM] hdu 5045 Contest (减少国家Dp)
  • 原文地址:https://www.cnblogs.com/xs104/p/4727461.html
Copyright © 2011-2022 走看看