2.2.1线性布局

线性布局的概念

线性布局(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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 11,797评论 0 17
  • 翻译自“Auto Layout Guide”。 2 自动布局细则手册 2.1 堆栈视图 接下来的章节展示了如何使用...
    lakerszhy阅读 5,858评论 3 9
  • 翻译自“Auto Layout Guide”。 1 入门 1.1 理解自动布局 自动布局根据视图层级结构中视图上的...
    lakerszhy阅读 9,148评论 3 26
  • 有一个叫保尔·柯察金的人出身贫寒,经常被人欺负。但是他自强不息,信念坚定。虽然后来病魔缠身。但是他勇斗病残,百折不...
    KayChan阅读 3,529评论 4 2
  • 转眼间,一年就这样悄然而逝啦!大学的一年,真的没有学到多少东西,吃喝玩乐,毫无意义,想学却又不想学,想走却又不知道...
    薰衣草的王国阅读 2,707评论 20 2