Android 之 线性布局

线性布局:
线性布局是最简单,Android开发者使用的最多的布局类型之一,开发者用它来组织
用户界面上的控件。线性布局的作用就像他的名字一样:它将控件组织在一个垂直或
水平的形式;

在线性布局中,左右的子元素都按照垂直或水平的顺序在界面上进行排列:
* 如果垂直排列,则:表现的形式为一列多行,且每行只包含一个界面元素;
* 如果水平排列:则:表现的形式为一行多列,且每列只包含一个界面元素;

案例如下:
效果实现:垂直排列,位于屏幕的右下角!





使用说明:
属性 对应方法 描述
android:orientation      setOrientation 设置线性布局的朝向,值:horizontal /vertical
android:gravity setGravity(int) 设置线性布局的内部元素的布局方式

给子控件赋权:(layout_weight)
与其他线性布局属性不同,其它属性应用在线性布局视图本身,而layout_weight是应用在在它的
子控件上的。权值本身应该是一个数字(如:0.5 0.25),如果把所有子空间的权值加起来等于1,
子控件的权值控制它在父线性布局中有多“重要”或者留给其多少"空间"

案例如下:
效果实现:垂直排列,按比重0.2 0.5 0.3 进行效果的实现;


<LinearLayout 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"
    android:orientation="vertical" >

    <!--
    效果实现:垂直排列,并在屏幕的右下角 

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

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

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

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


    <!-- 权值的使用:效果实现,垂直排列  比重:0.2 0.5 0.3 -->

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:background="#F5F5DC"
        android:gravity="center"
        android:text="权值:0.2\n 水平垂直居中" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.5"
        android:background="#00CED1"
        android:gravity="center"
        android:text="权值:0.5\n 水平垂直居中" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.3"
        android:background="#FFD700"
        android:gravity="center"
        android:text="权值:0.3\n 水平垂直居中" />

</LinearLayout>





示例图:

猜你喜欢

转载自sunzone.iteye.com/blog/1866668