天天看点

android学习笔记(8)linearlayout与android:layout_weight学习

一,linearlayout

线性布局,布局文件中设置多个linearlayout来达到整体线性布局的风格.

android:gravity="right"对齐方式,居右对齐,gravity是重心的意思

常用参数:center(居中),bottom(下),top(上),right(右),left(左)

二,android:layout_weight

给控件分配可权值,不分配默认为0

权值为0的控件按原来的方式给他布局;分配了权值的控件,在除去权值为0的控件之外的区域按权值布局,

例如:布局中有2个控件,一个android:layout_weight="2",另一个android:layout_weight="1"则整个屏幕内三分之二被第一个控件占据,剩下的三分之一被第二个控件占据.

例子:实现如图布局:

android学习笔记(8)linearlayout与android:layout_weight学习
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<LinearLayout
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- 水平布局 -->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
      <EditText
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="#ff0000"
          android:layout_weight="1">
      </EditText>
            
      <EditText
          android:layout_width="match_parent"
          android:layout_height="match_parent"  
          android:layout_weight="1" 
          android:background="#00ff00" >                            
       </EditText>
       <EditText
          android:layout_width="match_parent"
          android:layout_height="match_parent" 
          android:layout_weight="1" 
          android:background="#0000ff">                             
       </EditText>
    </LinearLayout>
        
    <!-- 垂直布局 -->
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_gravity="right">
       <EditText
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_weight="1" 
          android:background="#2F2F4F">                    
      </EditText>
      <EditText
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_weight="1" 
         android:background="#B87333">                    
      </EditText>
  </LinearLayout>
        
</LinearLayout>

</RelativeLayout>
           

继续阅读