LinearLayout线性布局实验

1. 实验内容简介

线性布局是最常用的布局方式。线性布局在XML布局文件中使用<LinearLayout>标签进行配置。线性布局可分为水平线性布局和垂直线性布局。通过android:orintation属性设置线性布局的方向,该属性的可取值是horizontal和vertical,默认值是horizontal。<LinearLayout>标签有一个非常重要的gravity属性,该属性用于控制布局中视图的位置。如果设置多个属性值,需要使用“|”进行分隔。在属性值和“|”之间不能有其他符号(例如,空格、制表符等)。

<LinearLayout>中的子标签还可以使用layout_gravity和layout_weight属性来设置每一个视图的位置。

layout_gravity:可取值与gravity属性相同,表示当前视图在布局中的位置。

layout_weight:是一个非负整数值,如果该属性值大于0,线性布局会根据水平或垂直方向以及不同视图的layout_weight属性值占所有视图的layout_weight属性值之和的比例为这些视图分配自己所占用的区域,视图将按相应比例拉伸。

2. UI界面布局

对应的大纲布局如下,含有多重嵌套布局:

 

3. XML代码编写和调试

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

   android:layout_width="fill_parent"

   android:layout_height="wrap_content"

   android:layout_gravity="center_vertical|center_horizontal"

   android:orientation="vertical" >

    <LinearLayout

       android:layout_width="match_parent"

       android:layout_height="wrap_content" >

        <LinearLayout

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_weight="1"

           android:orientation="vertical" >

            <Button

               android:id="@+id/button1"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="Button" />

       </LinearLayout>

 

        <LinearLayout

            android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_weight="1"

           android:orientation="vertical" >

            <Button

               android:id="@+id/button2"

                android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:layout_gravity="right"

               android:text="Button" />

       </LinearLayout>

</LinearLayout>

 

    <LinearLayout

        android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:gravity="center|center_vertical|center_horizontal">

        <Button

           android:id="@+id/button3"

           android:layout_width="wrap_content"

            android:layout_height="wrap_content"

           android:text="Button" />

    </LinearLayout>

 

    <LinearLayout

       android:layout_width="match_parent"

       android:layout_height="wrap_content" >

        <LinearLayout

           android:layout_width="wrap_content"

           android:layout_height="match_parent"

           android:layout_weight="1"

           android:orientation="vertical" >

            <Button

               android:id="@+id/button4"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

               android:text="Button" />

       </LinearLayout>

 

        <LinearLayout

           android:layout_width="wrap_content"

           android:layout_height="match_parent"

            android:layout_weight="1"

           android:orientation="vertical" >

            <Button

               android:id="@+id/button5"

               android:layout_width="wrap_content"

               android:layout_height="wrap_content"

                android:layout_gravity="right"

               android:text="Button" />

       </LinearLayout>

    </LinearLayout>

</LinearLayout>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值