Android 基础知识4-2.6LinearLayout(线性布局)

一、LinearLayout的概述

        线性布局(LinearLayout)主要以水平或垂直方式来排列界面中的控件。并将控件排列到一条直线上。在线性布局中,如果水平排列,垂直方向上只能放一个控件,如果垂直排列,水平方向上也只能方一个控件。

在这里插入图片描述

使用线性布局,需要将布局节点改成LinearLayout,基本格式如下:

<?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">
    
    ......

</LinearLayout>

二、LinearLayout常用属性

常用到的属性简单归纳一下:

属性1:android:orientation    指定线性布局的方向(水平或者垂直)

属性2:android:width             线性布局的容器宽度

属性3:android:height           线性布局的容器高度

属性4:android:background  线性布局的背景

属性5:android:gravity          线性布局中,子容器相对于父容器所在的位置

2.0 常见属性

2.属性值:

android:width="xxxdp"                          指定线性布局的容器宽度为:xxxdp

android:width="wrap_content"             指定线性布局的容器宽度为:根据容器内容宽度大小来填充屏幕宽度

android:width="match_parent"             指定线性布局的容器宽度为:撑满整个屏幕宽度

3.属性值:

android:height="xxxdp"                          指定线性布局的容器高度为:xxxdp

android:height="wrap_content"             指定线性布局的容器高度为:根据容器内容高度大小来填充屏幕高度

android:height="match_parent"             指定线性布局的容器高度为:撑满整个屏幕高度

4.属性值:

android:background="#000"                  指定线性布局的背景为:黑色(rgb颜色)

android:background="@android:color/black"   指定线性布局的背景为:黑色(引用android系统自带的原始黑色)

andrid:background="@color/colorPrimary"   指定线性布局的背景为:(根据res/color.xml 中的colorPrimary所定义的颜色设置)

2.1 orientation 属性

       在线性布局中,控件排列有水平和垂直两个方向,控件排列方向由android:orientation属性来控制,该属性需要加在LinearLayout标记的属性中。属性值:

android:orientation="horizontal"        指定线性布局方向:水平

android:orientation="vertical"        指定线性布局方向:垂直            

         从上图可以看出,将orientation属性值设置成为vertical控件将从垂直方向从上往下排列,将orientation属性值设置成为horizontal,控件将从水平方向从左往右排列。

2.2 gravity 属性

android:gravity="center"      指定线性布局中,子容器相对于父容器所在的位置为:正中心

android:gravity="cente_verticalr"      指定线性布局中,子容器相对于父容器所在的位置为:垂直方向的正中心

android:gravity="center_horizontal"      指定线性布局中,子容器相对于父容器所在的位置为:水平方向的正中心

android:gravity="left"      指定线性布局中,子容器相对于父容器所在的位置为:最左边(默认)

android:gravity="right"      指定线性布局中,子容器相对于父容器所在的位置为:最右边

android:gravity="top"      指定线性布局中,子容器相对于父容器所在的位置为:最上方(默认)

android:gravity="bottom"      指定线性布局中,子容器相对于父容器所在的位置为:最下方

        如上图:使用gravity属性值right|bottom,将LinearLayout布局中的控件放置于相对于父容器的右侧下方。

         线性方向:水平,可以通过控件的android:layout_gravity属性的值来改变控件在垂直方向上的位置。

         同理,线性方向:垂直,可以通过控件的android:layout_gravity属性的值来改变控件在水平方向上的位置。

2.3  layout_weight 属性

        LinearLayout中另外一个常用的属性是layout_weight ,该属性需要加在LinearLayout子控件中。其作用是分配线性布局中的剩余空间到该控件上。

       如下图所示,在控件没有添加layout_weight 属性时,控件未占满线性布局的区域会空出来。

在这里插入图片描述

 给控件button2加上android:layout_weight="1"后,会把线性布局剩余空间全部占满。

在这里插入图片描述

 如果给button1button2都加上android:layout_weight="1",则两个控件均匀分配剩余空间。

 在这里插入图片描述

         

         如果给button1加上android:layout_weight="3"button2加上android:layout_weight="1",则会实现将剩余的空间button1:button2会按照3:1的比例来分配这些剩余的空间。

在这里插入图片描述

 注意:剩余布局大小 = 父布局大小 - 子布局大小之和

  layout_weight权重属性,主要是用来分配控件排列会剩余的空间,而且也会根据属性值的大小来分配,比如view1的android:layout_weight="3",view2的android:layout_weight="2",那么剩余空间的分配就是view1:view2 是 3:2 。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
1. 创建一个新的Android Studio项目,并在activity_main.xml中打开设计视图。 2. 将LinearLayout拖放到屏幕上,并在属性检查器中设置以下属性: orientation:vertical(垂直方向) gravity:center(居中对齐) padding:16dp(内边距) 3. 在LinearLayout中添加一个TextView,用于显示应用程序名称或欢迎消息。 4. 接下来,使用两个EditText小部件添加一个LinearLayout,用于输入用户名和密码。为了使它们看起来更漂亮,可以使用drawable文件夹中的几个图标。 5. 紧接着,添加一个Button,用于提交登录或注册信息。可以使用selector文件夹中的几个不同状态的按钮,如pressed和focused。 6. 最后,添加一个TextView链接到注册页面,并在LinearLayout中设置gravity属性以居中对齐。 完成后,您的用户登录注册界面应该如此: ``` <LinearLayout android:orientation="vertical" android:gravity="center" android:padding="16dp" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:text="MyApp" android:textSize="24sp" android:textStyle="bold" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <LinearLayout android:orientation="vertical" android:layout_marginTop="24dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:hint="Username" android:drawableLeft="@drawable/ic_person_outline_black_24dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <EditText android:hint="Password" android:drawableLeft="@drawable/ic_lock_outline_black_24dp" android:inputType="textPassword" android:layout_marginTop="16dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout> <Button android:text="Login" android:textColor="#fff" android:background="@drawable/btn_bg" android:layout_marginTop="24dp" android:layout_width="match_parent" android:layout_height="wrap_content"/> <TextView android:text="Don't have an account? Register" android:textColor="@color/colorPrimaryDark" android:layout_marginTop="24dp" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yyxhzdm

你的鼓励是我创作的最大动力!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值