安卓布局

    安卓里面的布局方式主要有以下五种:LinearLayoutFrameLayoutRelativeLayoutAbsoluteLayoutTableLayout。一个简单的界面可以不使用多种布局,但是如果界面稍微复杂或者想要界面布局更加舒服好看很多时候是要多种布局嵌套使用的。下面简单介绍一下这五种布局。

       1、  LinearLayout :线性布局,按水平或垂直来排列所有的元素,即N行或者N列,不论元素的宽度或高度是多少。如果想要多行多列,只要在LinearLayout里面嵌套LinearLayout布局就好了。android:layout_weight 是线性布局里的一个属性,用来描述该布局里的子元素所占的比重大小,按比值分配,且数值越小所占的比重越高,如果只有一个元素则默认为0

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn01 :...">
    </Button>
    
     <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn02:...">
    </Button>
    
    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn03:...">
    	</Button>
    	
        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn04:...">
    	</Button>
    </LinearLayout>
</LinearLayout>

 

      2、FrameLayout:单帧布局。在这个布局中所有的组件都在该视图的左上角而无法指定位置。最后的效果就是后面的被前面的覆盖。

<FrameLayout 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">
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn00000000000"/>
     
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1111"/>
    
</FrameLayout>

    

      3、RelativeLayout:相对布局,最常用的布局。因为这种布局是把组件之间相互联系起来布局的,每个组件的位置都是依赖其他组件存在的。所以在这种布局里要用到最重要的就是组件的id

 

在这种布局里如果要改动某个组件的id,那就在所有与它有关的组件里都要改,否则会报错。那么组件之间怎么联系呢?很简单,无非就是左对齐右对齐顶端对齐之类,使用起来并不难。

<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=".RelativeActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button1"
        android:layout_toRightOf="@+id/button1"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/button2"
        android:layout_below="@+id/button3"
        android:text="TextView" />

</RelativeLayout>

 

 

 

 

     4、AbsoluteLayout:绝对布局。如名字所说这种布局确实很绝对,里面的每个组件都要求有这样两个属性,android:layout_xandroid:layout_y,用来描述组件的位置。这种布局并不好用,因为一款APP如果换了终端换了屏幕大小原来的布局就显得不合适了。

<AbsoluteLayout 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"
    tools:context=".AbsoluteActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="49dp"
        android:layout_y="137dp"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="194dp"
        android:layout_y="227dp"
        android:text="TextView" />
   
</AbsoluteLayout>

 

     5、TableLayout:表格布局。把整个界面分成表格一样的形式,每个组件占有相同的空间。TableLayout中包含许多的TableRowTableRow就相当于表格中的一行。如果不把组件放进TableRow中,那么就会占满整行。

 

 

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".TableActivity" >

   <TableRow>

       <Button
           android:id="@+id/button1"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button" />

       <Button
           android:id="@+id/button2"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button" />

       <Button
           android:id="@+id/button3"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button" />
  
   </TableRow>
   
   <TableRow>

       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button" />

       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button" />

       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Button" />
       
   </TableRow>

</TableLayout>

 

 

 

 

 

 

 

 

 

 

 

猜你喜欢

转载自scorates.iteye.com/blog/2100372