2.2.1线性布局

作者: EDU_MJ | 来源:发表于2017-10-24 23:13 被阅读0次

线性布局的概念

线性布局(LinearLayout)是Android开发中常用的一种布局方式,它是以垂直和水平方式来显示界面中的控件。 它以一对<LinearLayout></LinearLayout>标签进行标识。

常用属性


1 基本属性

属性 含义 属性值
layout_height wrap_content:包裹内容 match_parent:与父控件匹配
layout_width wrap_content:包裹内容 match_parent:与父控件匹配
orientation 方向 vertical:垂直 horizontal:水平
<LinearLayout  
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android: orientation ="vertical">
</LinearLayout> 

2 设置居中

在线性布局中,内部的控件可以通过layout_gravity属性控制对齐方式。

属性 含义
left
right
top
bottom
center_horizontal 水平居中
center_vertical 垂直居中
center 水平垂直居中

在垂直线性布局中,只能设置控件的水平对齐方式。在水平线性布局中,只能设置控件的垂直对齐方式

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="one"
        android:layout_gravity="left"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="two"
        android:layout_gravity="center_horizontal"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="three"
        android:layout_gravity="right"
        />
</LinearLayout>

3 设置控件比例

在线性布局中,内部的控件可以使用layout_weight权重属性来控制所占的比例。例如,在垂直线性布局中我们设置三个按钮的高度比例为1:2:3

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="one"
        android:layout_weight="1"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="two"
        android:layout_weight="2"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:text="three"
        android:layout_weight="3"
        />
</LinearLayout>

布局的嵌套

布局之间可以相互嵌套,形成较为复杂的布局。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="one"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="two"
            android:layout_weight="1"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="three"
            
            />
    </LinearLayout>
</LinearLayout>

相关文章

  • 2.2.1线性布局

    线性布局的概念 线性布局(LinearLayout)是Android开发中常用的一种布局方式,它是以垂直和水平方式...

  • 安卓原生页面布局总结

    布局分为线性布局:LinearLayout和相对布局:RelativeLayout 线性布局:LinearLayo...

  • Android之布局

    LinearLayout - 线性布局 线性布局,最常用的布局之一,所有包含在线性布局里的控件在线性方向上依次排列...

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

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

  • 常用的五大布局

    常用的五大布局(线性布局,相对布局,帧布局,表格布局,绝对布局) 1,线性布局 LinearLayout ...

  • 3.1 布局类Widget-线性布局Row和Column

    线性布局Row和Column弹性布局Felx 线性布局Row和Column 所谓线性布局,即指沿水平或垂直方向排布...

  • 四大layout

    LinerLayout 线性布局 LinerLayout, 中文名为线性布局。这个布局会将它所包含的控件在线性方向...

  • 布局 - 线性布局

    LinearLayout 线性布局 线性布局是一种非常常用的布局,控件在该布局中按线性方向依次排列。 属性 and...

  • 2019-03-15

    实验内容:关于线性布局、约束布局及表格布局的使用 主要代码: 主界面: 线性布局: 约束布局: 表格布局: 截图:...

  • 安卓布局详解

    今天要讲的布局就是线性布局、相对布局和约束布局 1.LinearLayout: -线性布局,两种排法:水平and...

网友评论

    本文标题:2.2.1线性布局

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