Android 开发基础(二):布局管理

(个人经验,仅供参考,错误之处,敬请谅解)

布局管理器

    这里只是大体介绍与给出示例,具体布局所需要用到的详细属性尚未列出。

  1. 线性布局(LinearLayout)

主要使用orientation 属性(horizontal、vertical)以及多层的嵌套方式来实现对页面的布局

    示例:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送短信"
            android:layout_gravity="center_horizontal"
            android:textSize="30sp"
            />
    <LinearLayout android:layout_width="match_parent"
                  android:orientation="horizontal"
                  android:layout_height="wrap_content">
        <TextView
                android:text="收件人"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <EditText android:layout_width="wrap_content"
                  android:hint="收件人电话"
                  android:layout_weight="2"
                  android:layout_height="wrap_content"/>
    </LinearLayout>

    <EditText android:layout_width="match_parent"
              android:hint="短信内容"
              android:gravity="top"
              android:layout_weight="1"
              android:layout_height="wrap_content"/>
    <LinearLayout android:layout_width="match_parent"
                  android:orientation="horizontal"
                  android:layout_height="wrap_content">
        <Button android:layout_width="wrap_content"
                android:text="发送"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
        <Button android:layout_width="wrap_content"
                android:text="取消"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
        <Button android:layout_width="wrap_content"
                android:text="清除"
                android:layout_weight="1"
                android:layout_height="wrap_content"/>
    </LinearLayout>

</LinearLayout>
  1. 相对布局(RelativeLayout)

通过设置不同组件之间的相对位置来进行布局(分为父子级、同级)

    示例:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送短信"
            android:textSize="30sp"
            android:id="@+id/v1"
            />

        <TextView
                android:layout_below="@id/v1"
                android:text="收件人"
                android:id="@+id/v2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <EditText android:layout_width="match_parent"
                  android:hint="收件人电话"
                  android:layout_below="@id/v1"
                  android:layout_toRightOf="@id/v2"
                  android:id="@+id/v3"
                  android:layout_height="wrap_content"/>


    <EditText android:layout_width="match_parent"
              android:hint="短信内容"
              android:gravity="top"
              android:layout_above="@+id/v4"
              android:layout_below="@id/v3"
              android:layout_height="wrap_content"/>

        <Button android:layout_width="wrap_content"
                android:text="发送"
                android:id="@+id/v4"
                android:layout_toLeftOf="@+id/v5"
                android:layout_alignParentLeft="true"
                android:layout_alignParentBottom="true"
                android:layout_height="wrap_content"/>
        <Button android:layout_width="wrap_content"
                android:text="取消"
                android:id="@+id/v5"
                android:layout_centerHorizontal="true"
                android:layout_alignParentBottom="true"
                android:layout_height="wrap_content"/>
        <Button android:layout_width="wrap_content"
                android:text="清除"
                android:layout_toRightOf="@id/v5"
                android:layout_alignParentRight="true"
                android:layout_alignParentBottom="true"
                android:layout_height="wrap_content"/>

</RelativeLayout>
  1. 表格布局(TableLayout)

通过使用表格型对页面进行布局(使用行列)

    示例:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:stretchColumns="*"
             android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送短信"
            android:textSize="30sp"
            />
	<TableRow>
	    <TextView
	            android:text="收件人"
	            android:layout_width="wrap_content"
	            android:layout_height="wrap_content"/>
	    <EditText android:layout_width="wrap_content"
	              android:hint="收件人电话"
	              android:layout_span="2"
	              android:layout_height="wrap_content"/>
	
	</TableRow>
    <EditText android:layout_width="match_parent"
              android:hint="短信内容"
              android:layout_weight="1"
              android:gravity="top"
              android:layout_height="wrap_content"/>
	<TableRow>
	    <Button android:layout_width="wrap_content"
	            android:text="发送"
	            android:layout_height="wrap_content"/>
	    <Button android:layout_width="wrap_content"
	            android:text="取消"
	            android:layout_height="wrap_content"/>
	    <Button android:layout_width="wrap_content"
	            android:text="清除"
	            android:layout_height="wrap_content"/>
	</TableRow>

</TableLayout>
  1. 帧布局(FrameLayout)

从页面左上角开始布局,主要使用空白元素(margin属性)

    示例:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="match_parent"
             android:layout_height="match_parent">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="发送短信"
            android:layout_marginTop="20dp"
            android:textSize="30sp"
            />

    <TextView
            android:text="收件人"
            android:layout_marginTop="40dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    <EditText android:layout_width="match_parent"
              android:hint="收件人电话"
              android:layout_marginTop="40dp"
              android:layout_marginLeft="50dp"
              android:layout_height="wrap_content"/>
    <EditText android:layout_width="match_parent"
              android:hint="短信内容"
              android:layout_marginTop="80dp"
              android:layout_marginBottom="40dp"
              android:gravity="top"
              android:layout_height="match_parent"/>

    <Button android:layout_width="wrap_content"
            android:text="发送"
            android:layout_gravity="bottom"
            android:layout_height="wrap_content"/>
    <Button android:layout_width="wrap_content"
            android:text="取消"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_height="wrap_content"/>
    <Button android:layout_width="wrap_content"
            android:text="清除"
            android:layout_gravity="bottom|right"
            android:layout_height="wrap_content"/>
</FrameLayout>
  1. 网格布局(GridLayout)

与表格布局类似,不同点在于可以跨行(rowspan)

【复用布局】
     通过写好样式文件(.xml)再进行引用:

<include layout="@layout/file_name"/>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值