http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 2511|回复: 5

[新手开发之旅] Android新手开发之旅-基本布局方式(四)

[复制链接]
发表于 2018-11-30 10:30:42 | 显示全部楼层 |阅读模式
本帖最后由 liu 于 2018-11-30 10:30 编辑
本节来了解网格布局(GridLayout)和绝对布局(AbsoluteLayout)



Android新手开发之旅【目录】



一、网格布局(GridLayout)

      
       GridLayout是在Android4.0中引进的新布局,GridLayout 的作用类似于HTML中的table标签,它把整个容器划分成rows x columns个网格,每个网格可以放置一个组件。(也可以设置跨行,跨列)

       属性:

       android:columnCount                        设置网格列数量
       android:rowCount                             设置网格行数量
       android:alignmentMode                     设置布局管理器的对齐方式
       android:columnOrderPreserved          设置网格容器是否保留列序号   
       android: rowOrderPreserved              设置网格管理器是否保留行序号      
       android:useDefaultMargins                 设置布局管理器是否使用页边距

      网格容器内子组件的xml属性:  

      layout_column                                   设置子组件在GridLayout的第几列   
      layout_columnSpan                            设置子组件在GridLayout上横向上跨几列   
      layout_gravity                                    设置子组件用何种方式占据改网格空间   
      layout_row                                        设置子组件在GridLayout的第几行     
      layout_rowspan                                 设置子组件在GridLayout纵向上跨几行

      这里用其中最几个常用的属性来举个例子(键盘上的数字键):      
      先上图:

      QQ截图20181130092848.png          

实现代码:         
[XML] 纯文本查看 复制代码
<?xml version="1.0" encoding="utf-8"?>[/color][/size][/font][font=Tahoma][size=4][color=#000000]<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="bottom|center_horizontal"
    android:paddingBottom="10dp"
    android:orientation="vertical">

    <GridLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:columnCount="4">

        <Button android:text="NumLock" />

        <Button android:text="/" />

        <Button android:text="*" />

        <Button android:text="-" />

        <Button android:text="7" />

        <Button android:text="8" />

        <Button android:text="9" />

        <Button android:text="+"
            android:layout_gravity="fill"
            android:layout_rowSpan="2"/>

        <Button android:text="4" />

        <Button android:text="5" />

        <Button android:text="6" />

        <Button android:text="1" />

        <Button android:text="2" />

        <Button android:text="3" />

        <Button android:text="Enter"
            android:layout_rowSpan="2"
            android:layout_gravity="fill"/>

        <Button android:text="0" />

        <Button android:text="."
            android:layout_gravity="fill"
            android:layout_columnSpan="2"/>

    </GridLayout>

</LinearLayout>
平均分配格行/列的问题:

GridLayout在Android5.1(API Level 21)时引入的android:layout_columnWeight和android:layout_rowWeight来解决平分问题,API21之前的需要使用兼容包:compile 'com.android.support:gridlayout-v7:26.0.0-alpha1'在布局中使用方法:
[XML] 纯文本查看 复制代码
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="4">

        <Button
            android:text="1"
            app:layout_columnWeight="1" />

        <Button
            android:text="2"
            app:layout_columnWeight="1" />

        <Button
            android:text="3"
            app:layout_columnWeight="1" />

        <Button
            android:text="4"
            app:layout_columnWeight="1" />

    </android.support.v7.widget.GridLayout>

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:columnCount="4">

        <Button android:text="1" />

        <Button android:text="2" />

        <Button android:text="3" />

        <Button android:text="4" />
    </GridLayout>
</LinearLayout>

    实现效果如下图所示:

               QQ截图20181130100736.png


二、绝对布局(AbsoluteLayout)

         
         绝对布局可以直接指定子元素的绝对位置,但是这个布局基本上用不到,因为手机屏幕差别比较大,所以适用性比较差 。        
       这里只简单介绍一下用法:

       属性:

        android:layout_x               设置组件的X坐标
        android:layout_y               设置组件的Y坐标

        示意图:

             QQ截图20181130101725.png

        实现代码:
[XML] 纯文本查看 复制代码
<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <AbsoluteLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_x="80dp"
            android:layout_y="200dp"
            android:text="你好啊" />
    </AbsoluteLayout>

</LinearLayout>
         
               














1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2018-11-30 11:04:14 | 显示全部楼层
强烈支持楼主ing……
发表于 2018-11-30 14:47:19 | 显示全部楼层
真是难得给力的帖子啊。
发表于 2018-11-30 16:33:13 | 显示全部楼层
楼主加油,我们都看好你哦。
发表于 2018-11-30 16:33:25 | 显示全部楼层
楼主的帖子不错, 继续加油哦
发表于 2018-11-30 16:40:02 | 显示全部楼层
加油楼主 等着你的更新呢
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-20 13:32

© 2014-2021

快速回复 返回顶部 返回列表