简单的LinearLayout线性布局

来源:互联网 发布:iphone刷机用什么软件 编辑:程序博客网 时间:2024/05/21 12:49

1. LinearLayout线性布局类似Swing中的流式布局

    android:orientation="vertical" :指定排列方式值:vertical(垂直)horizontal(水平)

    android:gravity="":控制所有子控件在父容器中的位置

    android:layout_width=""  布局宽度  wrap_content(只适应大小,强制性的使视图扩展以显示其全部内容)
    android:layout_height=""  布局高度   match_parent (填充布局单元)

    android:layout_weight="1" >   布局权重

    android:layout_gravity="bottom" 布局重心   bottom(下端),right (右边), bottom|right(右下角)

  

2.如图,如何实现该功能

    

3. 思路:

    左边按钮组:创建一个LinearLayout(为方便理解,用Lin1代替),将右上角按钮放入Lin1, 设置按钮的位置,  再创建一个LinearLayout(用Lin2代替),将右下角按钮放入Lin2, 设置按钮的位置,再将Lin1和Lin2 放入一个LinearLayout(用Lina代替)

   中间按钮:创建一个LinearLayout(用Linb代替),将中间按钮放入Lin3, 设为居中

   右边的按钮组:创建一个LinearLayout(为方便理解,用Lin4代替),将左上角按钮放入Lin4, 设置按钮的位置,  再创建一个LinearLayout(用Lin5代替),将左下角按钮放入Lin5, 设置按钮的位置,再将Lin4和Lin5 放入一个LinearLayout(用Linc代替)

   最后将Lina,Linb,Linc放入最外面那个LinearLayout 即可,我是用垂直做的,你也可以试试用水平的方法


4. 源代码解析:

<LinearLayout 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:orientation="horizontal"
    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:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="top"
                android:text="左上按钮" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:text="左下按钮" />
        </LinearLayout>
    </LinearLayout>


 <!-- 中间的按钮 -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1" >


        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="中间按钮" />
    </LinearLayout>


 <!-- 右边的按钮 -->

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|top"
                android:text="右上按钮" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="1" >


            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|bottom"
                android:text="右下按钮" />
        </LinearLayout>
    </LinearLayout>


</LinearLayout>
    


原创粉丝点击