Android studio下的线性布局(LinearLayout)与水平布局(ReativeLayout)详细解析+典型例子及其代码

文章介绍了Android中的线性布局和相对布局。线性布局包括水平和垂直方向,通过android:orientation属性切换,权重weight用于子元素分配空间。相对布局则利用各种对齐属性如centerInParent来定位元素,提供更灵活的布局方式。
摘要由CSDN通过智能技术生成

一:线性布局

线性布局有水平线性布局:android:orientation="horizontal"和垂直线性布局:android:orientation="vertical"两种布局。

当代码表示android:orientation="horizontal"时,表示这个布局下的所有子元素都要水平方向排列。

当代码表示android:orientation="verticall"时,表示这个布局下的所有子元素都要垂直方向排列。

说到方向orientation,必须也要提一下权重weight,这两个是密切联系的,我们经常看到android:layout_weight="1",表示什么意思呢?它代表的是这个布局下的所有子元素所占布局的比例。如果三个weight 都为1,说明各占均值1/3。

下面举个典型例子说明一下,

 这就是线性布局做出来的效果。

其完整代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="horizontal">
            <TextView
               android:layout_height="match_parent"
              android:layout_width="0dp"
                android:layout_weight="1"
               android:background="#00ff00"
                />
            <TextView
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:background="#ff0000"
                />
            <TextView
                android:layout_height="match_parent"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:background="#0000ff"
                />
        </LinearLayout>
<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#ff0000"
   />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#00ff00"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#0000ff"
        />

    </LinearLayout>
    </LinearLayout>

思路:一共有三个线性(LinearLayout)布局。其分别为大、中、小三个布局。大布局是整个虚拟手机页面,中布局也就是所组成的布局。更深一点解释为第一个布局也就是将虚拟器分为上下两个中布局,由于两个中布局是竖直向下排列的,其orizentation=”vertical”,即竖直向下。

接下来在上下两个小线性布局下面分别做情况讨论:

上面的小布局,目的是想要让它做三个水平排列的绿、红、蓝的权重皆为1的textview(视图)。因为是水平方向的,所以orizentation=“horizontal“,布局是往右延伸的,故高度是确定的,与父局一样;而宽度是不确定的,一般设为0dp,以权重值来具体划分宽度大小。其代码为height=”match_parent” width=”0dp” weight=”1”( 注意:这‘0dp和weight权重‘两个必须同时出现,否则会报错。

下面的小布局,目的是想要它做三个竖直排列的红、绿、蓝的权重皆为1的textview(视图)。因为是竖直向下排列的,所以orizentation=”vertical”。依次向下延伸,即宽度是确定的,高度不确定。故width=” match_parent”

Height=”0dp”weight=”1”

关于颜色,不知道你们都了解不,我也来说明一下吧:

#000000显示黑色,#ff0000显示红色,#00ff00显示绿色,#0000ff显示蓝色,#ffffff显示白色。

这只是其中一种表示方法,其他的方法感兴趣的话可以搜一搜。

二:相对布局

最常见的相对布局就是打游戏时的操控按钮。下面我也会举一下这个例子及其代码。

我们在相对布局下往往会用到很多代码,比如以下代码:

android:layout_centerInParent="true"  --将控件置于父控件的中心位置

android:layout_centerHorizontal="true"  --将控件置于水平方向的中心位置

android:layout_centerVertical="true"  --将控件置于垂直方向的中心位置

 属性值必须为id的引用名“@id/id-name”

android:layout_above意为在其id所在的元素的上方

 android:layout_below 意为在其id所在的元素的下方

android:layout_toLeftOf 意为在其id所在的元素的左边

android:layout_toRightOf 意为在其id所在的元素的右边

android:layout_alignTop 本元素的上边缘和其id所在的元素的上边缘对齐

android:layout_alignLeft 本元素的左边缘和其id所在的元素的左边缘对齐

android:layout_alignBottom 本元素的下边缘和其id所在的元素的下边缘对齐

android:layout_alignRight 本元素的右边缘和其id所在的元素的的右边缘对齐

哦对,还有一个知识点,<TextView下的android:layout_gravity表示的是整个TextView在布局下的位置。

下面为典型相对布局的例子:

游戏按钮的效果图:

完整代码如下:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="horizontal">
    <RelativeLayout
       android:layout_width="0dp"
        android:layout_height="match_parent"
      android:layout_weight="1"
        >
        <Button
            android:id="@+id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="⚪"
          android:layout_centerInParent="true"
            />
        <Button
            android:layout_above="@id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="↑"
            android:layout_alignLeft="@id/btn_center"
            android:textSize="25sp"

            />
        <Button
            android:layout_below="@id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="↓"
            android:layout_alignLeft="@id/btn_center"
            android:textSize="25sp"
            />
        <Button
            android:layout_toLeftOf="@id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="←"
           android:layout_alignTop="@id/btn_center"
            android:textSize="25sp"
            />
        <Button
            android:layout_toRightOf="@id/btn_center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="→"
            android:layout_alignTop="@id/btn_center"
            android:textSize="25sp"
            />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        >
        <Button
            android:id="@+id/btn_zhongquan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:text="A"
            />
        <Button

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toLeftOf="@id/btn_zhongquan"
            android:text="B"
            />
    </RelativeLayout>

</LinearLayout>

希望我的回答可以帮到你哦! 

线性布局(Linear Layout)是 Android 应用程序中最常用的布局之一,它按照从左到右(水平方向)或从上到下(垂直方向)的顺序排列其子视图。以下是关于线性布局的一些关键参数,针对垂直和水平方向: 1. **android:orientation属性**:这是决定布局方向的关键属性。对于垂直方向,你应该设置为`vertical`;对于水平方向,设置为`horizontal`。默认值是`horizontal`,即从左向右排列。 ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 垂直方向 --> <!-- 子视图内容 --> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <!-- 水平方向 --> <!-- 子视图内容 --> </LinearLayout> ``` 2. **android:gravity属性**:用于设置子视图在容器内的对齐方式。默认是`center_vertical|center_horizontal`,在垂直布局中表示居中对齐,而在水平布局中,如果没有指定其他方向,也会默认居中。如果你想要在垂直方向顶部对齐,可以设置为`top`;底部对齐则为`bottom`。 3. **android:weightSum属性**:用于分配给子视图在水平方向的空间占比。当设置了权重(android:layout_weight)后,布局系统会根据这个属性来调整每个子视图的宽度。 4. **android:layout_weight和android:layout_width属性**:在水平布局中,`android:layout_width="0dp"`加上`android:layout_weight="1"`会使视图根据`android:weightSum`动态分配空间。 5. **android:padding 和 margin 属性**:这些属性控制了子视图与周围边界的间距,可以影响布局的整体外观。 了解并灵活运用这些参数可以帮助你精确地控制线性布局中各元素的位置和大小。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值