0
点赞
收藏
分享

微信扫一扫

Android应用开发之线性布局

代码敲到深夜 2022-03-12 阅读 67

在这里插入图片描述

Android中有六大布局,分别是:

  • LinearLayout(线性布局)
  • RelativeLayout(相对布局)
  • TableLayout(表格布局)
  • FrameLayout(帧布局)
  • AbsoluteLayout(绝对布局)
  • GridLayout(网格布局)

今天我们要讲解的就是第一个布局,LinearLayout(线性布局)

LinearLayout

LinearLayout又称作线性布局,是一种非常常用的布局。

这个布局会将它所包含的控件在线性方向上依次排列。

既然是线性排列,肯定就不仅只有一个方向,这里一般只有两个方向:水平方向和垂直方向。

属性

LinearLayout(线性布局)常用到的属性简单归纳一下:

属性名解释
android:orientation指定线性布局的方向(水平或者垂直)
android:width线性布局的容器宽度
android:height线性布局的容器高度
android:background线性布局的背景
android:gravity线性布局中,子容器相对于父容器所在的位置
android:layout_gravity容器相对它的父元素的对齐方式
android:layout_weight权重,按比例来分配控件占用父控件的大小
orientation
属性值解释
android:orientation=“horizontal”指定线性布局方向:水平
android:orientation=“vertical”指定线性布局方向:垂直
width
属性值解释
android:width=“xxxdp”指定线性布局的容器宽度为:xxxdp
android:width=“wrap_content”指定线性布局的容器宽度为:根据容器内容宽度大小来填充屏幕宽度
android:width=“match_parent”指定线性布局的容器宽度为:撑满整个屏幕宽度
height
属性值解释
android:height=“xxxdp”指定线性布局的容器高度为:xxxdp
android:height=“wrap_content”指定线性布局的容器高度为:根据容器内容高度大小来填充屏幕高度
android:height=“match_parent”指定线性布局的容器高度为:撑满整个屏幕高度
background
属性值解释
android:background="#000"指定线性布局的背景为:黑色(rgb颜色)
android:background="@android:color/black"指定线性布局的背景为:黑色(引用android系统自带的原始黑色)
andrid:background="@color/colorPrimary"指定线性布局的背景为:(根据res/color.xml 中的colorPrimary所定义的颜色设置)
gravity

自身是父容器

属性值解释
android:gravity=“center”指定线性布局中,子容器相对于父容器所在的位置为:正中心
android:gravity=“cente_verticalr”指定线性布局中,子容器相对于父容器所在的位置为:垂直方向的正中心
android:gravity=“center_horizontal”指定线性布局中,子容器相对于父容器所在的位置为:水平方向的正中心
android:gravity=“left”指定线性布局中,子容器相对于父容器所在的位置为:最左边(默认)
android:gravity=“right”指定线性布局中,子容器相对于父容器所在的位置为:最右边
android:gravity=“top”指定线性布局中,子容器相对于父容器所在的位置为:最上方(默认)
android:gravity=“bottom”指定线性布局中,子容器相对于父容器所在的位置为:最下方
layout_gravity

自身是子容器

属性值解释
android:gravity=“center”指定线性布局中,子容器相对于父容器所在的位置为:正中心
android:gravity=“cente_verticalr”指定线性布局中,子容器相对于父容器所在的位置为:垂直方向的正中心
android:gravity=“center_horizontal”指定线性布局中,子容器相对于父容器所在的位置为:水平方向的正中心
android:gravity=“left”指定线性布局中,子容器相对于父容器所在的位置为:最左边(默认)
android:gravity=“right”指定线性布局中,子容器相对于父容器所在的位置为:最右边
android:gravity=“top”指定线性布局中,子容器相对于父容器所在的位置为:最上方(默认)
android:gravity=“bottom”指定线性布局中,子容器相对于父容器所在的位置为:最下方
layout_weight

当我们给一个view设置了android:layout_weight属性,意味着赋予它话语权,常规思维就是谁的weight大,谁说了算(空间占比大)。

属性值解释
android:layout_weight=“2”该单元权重为2

实例代码

activity_main.xml:

<?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:background="#F6F6F4"
    android:orientation="vertical"
    android:gravity="top">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#000000"
        android:orientation="horizontal"
        android:gravity="center"
        android:layout_weight="3">

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            android:layout_gravity="bottom"/>

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

        <Button
            android:id="@+id/button6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#CC1818"
        android:layout_weight="1"></LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#0C56EA"
        android:layout_weight="3"></LinearLayout>
</LinearLayout>

运行效果:

在这里插入图片描述

举报

相关推荐

0 条评论