当前位置: 首页 > news >正文

【Android程序开发】常用布局--线性布局LinearLayout

前几个小节的例程中,XML文件用到了LinearLayout布局,它的学名为线性布局。顾名思义,线性布局 像是用一根线把它的内部视图串起来,故而内部视图之间的排列顺序是固定的,要么从左到右排列,要 么从上到下排列。在XML文件中,LinearLayout通过属性android:orientation区分两种方向,其中从左 到右排列叫作水平方向,属性值为horizontal;从上到下排列叫作垂直方向,属性值为vertical。如果LinearLayout标签不指定具体方向,则系统默认该布局为水平方向排列,也就是默认 

android:orientation="horizontal"

下面做个实验,让XML文件的根节点挂着两个线性布局,第一个线性布局采取horizontal水平方向,第 二个线性布局采取vertical垂直方向。然后每个线性布局内部各有两个文本视图,通过观察这些文本视图 的排列情况,从而检验线性布局的显示效果。详细的XML文件内容如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

        <TextView

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"

            android:text="横排第一个"

            android:textSize="17sp"

            android:textColor="#000000" />

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="横排第二个"

            android:textSize="17sp"

            android:textColor="#000000" />

    </LinearLayout>

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="竖排第一个"

            android:textSize="17sp"

            android:textColor="#000000" />

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="竖排第二个"

            android:textSize="17sp"

            android:textColor="#000000" />

    </LinearLayout>
</LinearLayout>

运行测试App,进入如下图所示的演示页面,可见horizontal为横向排列,vertical为纵向排列,说明android:orientation的方向属性确实奏效了。

除了方向之外,线性布局还有一个权重概念,所谓权重,指的是线性布局的下级视图各自拥有多大比例 的宽高。比如一块蛋糕分给两个人吃,可能两人平均分,也可能甲分三分之一,乙分三分之二。两人平 均分的话,先把蛋糕切两半,然后甲分到一半,乙分到另一半,此时甲乙的权重比为1:1。甲分三分之 一、乙分三分之二的话,先把蛋糕平均切成三块,然后甲分到一块,乙分到两块,此时甲乙的权重比为

1:2。就线性布局而言,它自身的尺寸相当于一整块蛋糕,它的下级视图们一起来分这个尺寸蛋糕,有的 视图分得多,有的视图分得少。分多分少全凭每个视图分到了多大的权重,这个权重在XML文件中通过 属性

android:layout_weight

来表达。

把线性布局看作蛋糕的话,分蛋糕的甲乙两人就相当于线性布局的下级视图。假设线性布局平均分为左 右两块,则甲视图和乙视图的权重比为1:1,意味着两个下级视图的layout_weight属性都是1。不过视图 有宽高两个方向,系统怎知layout_weight表示哪个方向的权重呢?所以这里有个规定,一旦设置了layout_weight属性值,便要求layout_width填0dp或者layout_height填0dp。如果layout_width填0dp,则layout_weight表示水平方向的权重,下级视图会从左往右分割线性布局;如果layout_height填0dp,则layout_weight表示垂直方向的权重,下级视图会从上往下分割线性布局。 按照左右均分的话,线性布局设置水平方向horizontal,且甲乙两视图的layout_width都填0dp,layout_weight都填1,此时横排的XML片段示例如下:

<LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="horizontal">

    <TextView

              android:layout_width="0dp"

              android:layout_height="wrap_content"

              android:layout_weight="1"

              android:text="横排第一个"

              android:textSize="17sp"

              android:textColor="#000000" />

    <TextView

              android:layout_width="0dp"

              android:layout_height="wrap_content"

              android:layout_weight="1"

              android:text="横排第二个"

              android:textSize="17sp"

              android:textColor="#000000" />
</LinearLayout>

 

按照上下均分的话,线性布局设置垂直方向vertical,且甲乙两视图的layout_height都填0dp,

layout_weight都填1,此时竖排的XML片段示例如下:

<LinearLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:orientation="vertical">

    <TextView

              android:layout_width="wrap_content"

              android:layout_height="0dp"

              android:layout_weight="1"

              android:text="竖排第一个"

              android:textSize="17sp"

              android:textColor="#000000" />

    <TextView

              android:layout_width="wrap_content"

              android:layout_height="0dp"

              android:layout_weight="1"

              android:text="竖排第二个"

              android:textSize="17sp"

              android:textColor="#000000" />
</LinearLayout>

把上面两个片段放到新页面的XML文件,其中第一个是横排区域采用红色背景(色值为ff0000),第二 个是竖排区域采用青色背景(色值为00ffff)。重新运行测试App,打开演示界面如下图所示,可见横 排区域平均分为左右两块,竖排区域平均分为上下两块。

 

感谢观看!!!

相关文章:

  • 基于Dijkstra、A*和动态规划的移动机器人路径规划(Matlab代码实现)
  • 国产EDA与FPGA芯片验证方案
  • 一种更优雅书写Python代码的方式
  • 自定义类型(结构体、位段、联合体、枚举)
  • 如何基于 GORM 实现 CreateOrUpdate 方法
  • Spring Boot核心之基本配置、日志配置、自动配置、条件注解
  • ArcGIS校园3D展示图制作详细教程
  • 【算法 | 实验6-1】n*n的网格,从左上角开始到右下角结束遍历所有的方块仅一次,总共有多少种不同的遍历路径
  • c++数据结构:图(邻接表)
  • SCOUT MINI Pro松灵机器人j建图定点步骤
  • 10个Pandas的小技巧
  • 广度优先遍历解决迷宫问题
  • 掌握 Microsoft Excel 宏和 Excel VBA
  • springboot框架中如何整合mybatis框架?
  • 阿里巴巴面试题- - -JVM篇(十七)
  • JS函数式编程 数组部分风格 ES6版
  • KMP算法及优化
  • PHP面试之三:MySQL数据库
  • Redux系列x:源码分析
  • SegmentFault 技术周刊 Vol.27 - Git 学习宝典:程序员走江湖必备
  • 当SetTimeout遇到了字符串
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 前端面试总结(at, md)
  • 如何将自己的网站分享到QQ空间,微信,微博等等
  • 如何正确配置 Ubuntu 14.04 服务器?
  • 网络应用优化——时延与带宽
  • 微信小程序开发问题汇总
  • 为视图添加丝滑的水波纹
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • elasticsearch-head插件安装
  • hi-nginx-1.3.4编译安装
  • ​​​​​​​​​​​​​​汽车网络信息安全分析方法论
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • (一)Dubbo快速入门、介绍、使用
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • (转)Java socket中关闭IO流后,发生什么事?(以关闭输出流为例) .
  • .net core使用RPC方式进行高效的HTTP服务访问
  • .NET Micro Framework初体验(二)
  • .Net(C#)常用转换byte转uint32、byte转float等
  • .NET简谈设计模式之(单件模式)
  • @基于大模型的旅游路线推荐方案
  • [100天算法】-x 的平方根(day 61)
  • [2013][note]通过石墨烯调谐用于开关、传感的动态可重构Fano超——
  • [Android Pro] listView和GridView的item设置的高度和宽度不起作用
  • [AUTOSAR][诊断管理][ECU][$37] 请求退出传输。终止数据传输的(上传/下载)
  • [C#]手把手教你打造Socket的TCP通讯连接(一)
  • [C/C++] -- 二叉树
  • [C++]Leetcode17电话号码的字母组合
  • [CSS3备忘] transform animation 等
  • [flask] flask的基本介绍、flask快速搭建项目并运行
  • [Flex][问题笔记]TextArea滚动条问题
  • [github全教程]github版本控制最全教学------- 大厂找工作面试必备!
  • [hdu 1247]Hat’s Words [Trie 图]
  • [IDF]聪明的小羊