---
title: "Android LinearLayout 线性布局"
author: "bing0313"
date: "2013-06-16T10:14:15Z"
tags: []
categories: ["移动开发"]
slug: "android-linearlayout"
draft: false
lastmod: "2013-06-11T11:14:40Z"
id: 1885352
url: "https://bing0313.iteye.com/blog/1885352"
---

LinearLayout 线性布局
LinearLayout 按照垂直或者水平的顺序依次排列子元素，每一个子元素位于前一个元素之后。
LinearLayout 中的子元素属性：android：layout_weight生效，它用于描述该子元素在剩余空间中占有的大小比例，加入一行只有一个文本框，那么它的默认值就是0，如果一行中有两个等长的文本框，那么他们的android:layout_weight值可以同为：1，如果一行中有两个不等长的文本框，那么他们的android:layout_weight值分别为：1和2 ，那么第一个文本框将占据剩余空间的三分之二，第二个文本框将占剩余空间的三分之一，android:layout_weight遵循数值越小，重要度越高的原则； 
效果图：

![](http://dl2.iteye.com/upload/attachment/0085/6620/cb18425c-8e2b-3dd0-8aa1-1bb73aa60a2f.png)


```xml

    <!--  
        布局之一：线性布局   LinearLayout  
                横向布局：一行多列           纵向布局：一列多行  
        LinearLayout的属性：  
            android:background                  设置整个布局画面的背景  
            android:orientation="horizontal"    子元素的排列队形，是横向排列还是纵向排列  
            android:gravity="bottom"            子元素在布局中的缺省(默认)对齐方式  
            android:padding                     设置子元素的彼次连接，中间不留空白  
              
        子元素的属性：  
            android:layout_gravity          设置自身对象在父布局中的看齐方式，可以更新父布局对象给定的缺省的值  
            android:layout_weight           将父布局中剩余的尺寸按各兄弟元素的weight值比例进行填充；  
    -->  
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

  <LinearLayout
      android:orientation="horizontal"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:layout_weight="1">
      <TextView
          android:text="red"
          android:gravity="center_horizontal"
          android:background="#aa0000"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="green"
          android:gravity="center_horizontal"
          android:background="#00aa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="blue"
          android:gravity="center_horizontal"
          android:background="#0000aa"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
      <TextView
          android:text="yellow"
          android:gravity="center_horizontal"
          android:background="#aaaa00"
          android:layout_width="wrap_content"
          android:layout_height="fill_parent"
          android:layout_weight="1"/>
  </LinearLayout>
	
  <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1">
    <TextView
        android:text="row one"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row two"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row three"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <TextView
        android:text="row four"
        android:textSize="15pt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
  </LinearLayout>

</LinearLayout>
     
```