Android线性布局

作者: 牧童遥指2000 | 来源:发表于2016-11-28 22:55 被阅读55次

线性布局

LinearLayout 是一个视图组,用于使所有子视图在单个方向(垂直或水平)保持对齐。 您可以使用 Android:orientation 属性指定布局方向。
LinearLayout 的所有子视图依次堆叠,因此无论子视图有多宽,垂直列表每行均只有一个子视图,水平列表将只有一行高(最高子视图的高度加上内边距)。 LinearLayout 遵守子视图之间的“边距”以及每个子视图的“重力”(右对齐、居中对齐、左对齐)。

布局权重

权重相等的子视图 要创建一个线性布局,让每个子视图在屏幕上都占据相同的空间量,则将每个视图的 android:layout_height 均设置为 “0dp”(对于垂直布局),或将每个视图的android:layout_width 均设置为 “0dp”(对于水平布局)。 然后,将每个视图的 android:layout_weight 均设置为 “1”。 LinearLayout 还支持使用 android:layout_weight 属性为各个子视图分配权重。此属性根据视图应在屏幕上占据的空间量向视图分配“重要性”值。 权重值更大的视图可以填充父视图中任何剩余的空间。子视图可以指定权重值,然后系统会按照子视图声明的权重值的比例,将视图组中的任何剩余空间分配给子视图。 默认权重为零。
例如,如果有三个文本字段,其中两个声明权重为 1,另一个没有赋予权重,则没有权重的第三个文本字段将不会扩展,并且仅占据其内容所需的区域。 另外两个文本字段将以同等幅度进行扩展,以填充所有三个字段都测量后还剩余的空间。 如果为第三个字段提供权重 2(而非 0),那么相当于声明现在它比其他两个字段更为重要,因此,它将获得总剩余空间的一半,其他两个均享余下空间。

示例

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="16dp" android:paddingRight="16dp" android:orientation="vertical" > <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/to" /> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="@string/subject" /> <EditText android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:gravity="top" android:hint="@string/message" /> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:text="@string/send" /></LinearLayout>

我的微信二维码如下,欢迎交流讨论


这里写图片描述

欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧
微信订阅号二维码如下:


这里写图片描述

相关文章

  • Android - 4种基本布局

    1.线性布局 LinearLayout:线性布局 EditText 和 Button 的 android:layo...

  • Android应用界面开发——第二周笔记

    线性布局 线性布局是程序中常见的布局方式之一,包括水平线性布局和垂直线性布局两种, 通过Android:orien...

  • 2020-10-06

    Android常见界面布局:RelativeLayout(相对布局) LinearLayout(线性...

  • 常用的UI布局

    线性布局 http://schemas.android.com/apk/res/android" android:...

  • 基础布局

    Android中的布局 线性布局:LinerLayout 表格布局:TableLayout 相对布局:Relati...

  • Android控件

    vertial 水平horizontal 垂直 布局 : LinearLayout 线性布局android:l...

  • 线性布局(LinearLayout)

    线性布局,顾名思义,指的是整个Android布局中的控件摆放方式是以线性的方式摆放的 线性布局排列方式有: 纵向:...

  • 2018-08-29

    Android基础知识点整理 一、Android六大布局 1.1 LinearLayout线性布局 android...

  • Android常见布局

    在Android中免不了用到布局,今天主要学习下Android常见布局 线性布局(LinearLayout) 帧布...

  • Android基础01

    Android中有六大布局,分别是:LinearLayout(线性布局),RelativeLayout(相对布局)...

网友评论

    本文标题: Android线性布局

    本文链接:https://www.haomeiwen.com/subject/hvqapttx.html