android 59 LinearLayout 线性布局

##常见的布局
* LinearLayout 线性布局
线性布局往左右拉是拉不动的,
> 线性布局的朝向 vertical|horizontal
> 线性布局的权重 weight 和 0dip一起使用

<?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:orientation="vertical" >  垂直排列,里面的内容垂直排列

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dip"  高度为0,则高度根据权重来占比例,
        android:layout_weight="1"  权重为1,则高度为权重和的1份即1/3,权重只能跟为0的宽度或高度,
        android:text="按钮1" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="按钮2" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:text="按钮3" />

</LinearLayout>
<?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:orientation="horizontal" >   水平排列,不用权重则多的将会挤出去,

    <Button
        android:layout_width="0dip"  宽度为0,
        android:layout_height="wrap_content"  高度填充父窗体,和父组件宽度一样宽,
        android:layout_weight="2"    权重为2,则宽度为权重和的2份即2/4,权重只能跟为0的宽度或高度,
        android:text="按钮1" />

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"   权重为1,则宽度为权重和的1份
        android:text="按钮2" />

    <Button
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="1"        权重为1,则宽度为权重和的1份
        android:text="按钮3" />

</LinearLayout>

 

 以上的代码:

<?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:orientation="vertical" >   最外层是线性布局,垂直排列

    <LinearLayout
        android:orientation="horizontal"     里面是水平排列
        android:layout_width="match_parent"  宽度为父窗体宽度
        android:layout_weight="1"       权重为1
        android:layout_height="0dip" >  写0可以用权重了
        <TextView 
               android:layout_width="0dip"      宽度为0
        android:layout_height="fill_parent"     高度为父窗体
        android:layout_weight="1"              宽度权重为1
            android:background="#ff0000"
            />
        <TextView 
               android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
            android:background="#00ff00"
            />
        <TextView 
               android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
            android:background="#0000ff"
            />
        
    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"     里面垂直排列
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dip" >
          <TextView 
               android:layout_width="fill_parent"   宽度为父窗体
        android:layout_height="0dip"        高度为0
        android:layout_weight="1"           高度权重为1
            android:background="#ff0000"
            />
        <TextView 
               android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
            android:background="#00ff00"
            />
        <TextView 
              android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
            android:background="#0000ff"
            />
        
        
    </LinearLayout>

</LinearLayout>
原文地址:https://www.cnblogs.com/yaowen/p/4915477.html