Android--界面布局(线性布局、相对布局)

1、线性布局的子控件通常被指定为水平或者竖直排列。在XML布局文件中定义LinearLayout的基本语法格式如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 属性="属性值" ......>
</LinearLayout>

LinearLayout中常见的两个属性:

①android:orientattion用于设置LinearLayout中控件的排列顺序,其可选值为vertical和horizonal.

vertical:表示LinearLayout内控件从上到下依次竖直排列。

horizonal:表示LinearLayout内控件从左到右依次水平排列。

②android:layout_weight该属性表示权重,通过设置该属性值,可使布局内的控件按照权重比显示大小,在进行屏幕适配时起到关键作用。

例题:依据图像设置的按钮位置,设计代码,运行效果。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮1"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮2"
            android:layout_marginLeft="75dp"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="按钮3" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal">
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮4"/>
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="按钮5"
            android:layout_marginLeft="75dp"/>
    </LinearLayout>
</LinearLayout>

2、相对布局通过相对定位的方式指定子控件的位置。在XML布局文件中定义RelativeLayout时使用<RelativeLayout>标签,定义格式如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 属性="属性值" ......>
</RelativeLayout>

在RelativeLayout中的子控件具备一些属性,用于指定子控件的位置,这些子控件属性如下表所示:

属性名称

功能描述

android:layout_centerInParent

设置当前控件位于父布局的中央位置

android:layout_centerVertical

设置当前控件位于父布局的垂直居中位置

android:layout_centerHorizontal

设置当前控件位于父布局的水平居中位置

android:layout_above

设置当前控件位于某控件的上方

android:layout_below

设置当前控件位于某控件的下方

android:layout_toLeftOf

设置当前控件位于父布局的左侧

android:layout_toRightOf

设置当前控件位于父布局的右侧

android:layout_alignParentTop

设置当前控件是否与父控件顶端对齐

android:layout_alignParentBottom

设置当前控件是否与父控件底端对齐

android:layout_alignParentLeft

设置当前控件是否与父控件左对齐

android:layout_alignParentRight

设置当前控件是否与父控件右对齐

android:layout_alignTop

设置当前控件的上边界与某控件的上边界对齐

android:layout_alignBottom

设置当前控件的下边界与某控件的下边界对齐

android:layout_alignLeft

设置当前控件的左边界与某控件的左边界对齐

android:layout_alignRight

设置当前控件的右边界与某控件的右边界对齐

例题:依据图像设置的按钮位置,设计代码,运行效果。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="100dp"
        />
    <Button
        android:id="@+id/btn_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_below="@id/btn_one"
        android:layout_toRightOf="@id/btn_one"
        />
    <Button
        android:id="@+id/btn_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_above="@id/btn_three"
        android:layout_toRightOf="@id/btn_three"
        />
    <Button
        android:id="@+id/btn_four"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮4"
        android:layout_below="@id/btn_three"
        android:layout_toLeftOf="@id/btn_three"
        />
    <Button
        android:id="@+id/btn_five"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮5"
        android:layout_below="@id/btn_three"
        android:layout_toRightOf="@id/btn_three"
        />
    </RelativeLayout>

猜你喜欢

转载自blog.csdn.net/weixin_65089091/article/details/129033723