六大布局之LinearLayout

1. 什么是Layout?

Layout——界面布局,为应用程序提供界面架构。控制Activity中控件的大小、位置、颜色等属性的方法.php

Layout 与 ViewGroup的关系android

img

  • ViewGroup是一个容器,继承自View.
  • ViewGroupLayout和一些其它组件的基类.

在Android中提供了几个经常使用布局: LinearLayout 线性布局bash

RelativeLayout相对布局架构

FrameLayout 帧布局ide

AbsoluteLayout绝对布局布局

TableLayout 表格布局ui

GridLayout网格布局this

今天咱们主要讲线性布局,其他的经常使用布局会在后期文章为你们详细讲述。spa

2. LinearLayout线性布局:

指子控件以水平或垂直方式排列,正如其名字同样,这个布局中的全部控件在线性方向上依次排列。3d

经常使用属性:

  1. android:id:为该组件添加一个资源id,即标识符,能够经过id来找到该布局或者控件。
  2. android:layout_width:布局的宽度,用wrap_content表示组件的实际宽度,match_parent表示填充父容器
  3. android:layout_height:布局的长度,用wrap_content表示组件的实际长度,match_parent表示填充父容器
  4. android:orientation:布局中的排列方式,有两种方式:horizontal水平,vertical竖直,若是不设置则默认水平显示
  5. android:gravity:控制组件所包含的子元素的对齐方式
  6. android:layout_gravity:控制该组件在父容器里的对齐方式
  7. android:background:为该组件添加一个背景图片或者背景颜色,颜色常以六位的十六进制表示
  8. android:layout_margin :外边距,布局或控件距离外部元素的边距
  9. android:layout_padding :内边距,布局或控件距离内部元素的边距
  10. android:layout_weight:权重,除了被显示占据的空间之外的的空间,而后根据权重的大小来分配空间,使用权重一般会把分配该权重方向的宽度设置为0dp,若是未设置0dp,则该控件会占据指定的宽度,而后再加上根据权重来分配的空间

下面依次分别举例说明使用方法

orientation 是一个视图组,能够在一个方向垂直或者水平分布全部子项

android:orientation="vertical" 时, 只有水平方向的设置才起做用,垂直方向的设置不起做用.即:left,right,center_horizontal 是生效的. 当 android:orientation="horizontal" 时, 只有垂直方向的设置才起做用,水平方向的设置不起做用.即:top,bottom,center_vertical 是生效的.

<?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="@color/white" android:orientation="vertical">

    <TextView android:id="@+id/tv_here" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里1" android:textColor="@color/black" android:textSize="16sp" />

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里2" android:textColor="@color/black" android:textSize="16sp" />
</LinearLayout>
复制代码

img

<?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="@color/white" android:orientation="horizontal">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里1" android:textColor="@color/black" android:textSize="16sp" />

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里2" android:textColor="@color/black" android:textSize="16sp" />
</LinearLayout>
复制代码

img

gravity:

android:layout_gravity是本(子)元素相对于父元素的对齐方式设置在子元素上. android:gravity="bottom|right"是本(父)元素全部子元素的对齐方式,设置在父元素上,多个值用 | 隔开.

其属性值分别为:center(总体居中)、center_vertical(垂直居中)、center_horizontal(水平居中)、right(居右)、left(居左)、bottom(底部)和top(顶部)

<?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="@color/white" android:gravity="center" android:orientation="vertical">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这里1" android:textColor="@color/black" android:textSize="16sp" />

    <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@color/color_F9658E" android:gravity="bottom" android:text="这里2" android:textColor="@color/black" android:textSize="16sp" />

    <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@color/color_A9BEFF" android:gravity="center_vertical|right" android:text="这里3" android:textColor="@color/black" android:textSize="16sp" />
</LinearLayout>
复制代码

img

background

<?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="@mipmap/ic_launcher" android:orientation="vertical">

    <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:background="@color/color_FF6F65" android:gravity="center" android:text="这里1" android:textColor="@color/black" android:textSize="16sp" />
</LinearLayout>
复制代码

img

padding && margin: android:padding="10dp" (是本元素全部子元素的与父元素边缘的距离,设置在父元素上). android:layout_marginLeft="10dp"(子元素与父元素边缘的距离,设置在子元素上).

<?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="@color/white" android:gravity="center" android:orientation="vertical">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/color_FF6F65" android:text="这里1" android:textColor="@color/black" android:textSize="16sp" />

    <TextView android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="@color/color_F9658E" android:gravity="bottom" android:padding="15dp" android:text="这里2" android:textColor="@color/black" android:textSize="16sp" />

    <TextView android:layout_width="100dp" android:layout_height="100dp" android:background="@color/color_A9BEFF" android:gravity="center_vertical|right" android:paddingRight="15dp" android:text="这里3" android:textColor="@color/black" android:textSize="16sp" />
</LinearLayout>
复制代码

img

weight: android:layout_weight ="1"(线性布局内子元素对未占用空间【水平或垂直】分配权重值,其值越小,权重越大. 前提是子元素设置了android:layout_width = "match_parent" 属性 ( 水平方向 )或 android:layout_height = "match_parent"属性( 垂直方向). 如 果 某 个 子 元 素的android:layout_width = "0dp"android:layout_height="0dp" ,则 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:background="@color/white" android:gravity="center" android:orientation="vertical">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="10dp" android:background="@color/color_FF6F65" android:paddingLeft="10dp" android:paddingTop="20dp" android:paddingRight="40dp" android:paddingBottom="60dp" android:text="这里1" android:textColor="@color/black" android:textSize="16sp" />

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">

        <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="1" android:background="@color/color_F9658E" android:paddingLeft="15dp" android:text="这里2" android:textColor="@color/black" android:textSize="16sp" />

        <TextView android:layout_width="match_parent" android:layout_height="50dp" android:layout_weight="2" android:background="@color/color_A9BEFF" android:paddingRight="15dp" android:text="这里3" android:textColor="@color/black" android:textSize="16sp" />
    </LinearLayout>

    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content">

        <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:background="@color/color_29CFFF" android:paddingLeft="15dp" android:text="这里4" android:textColor="@color/black" android:textSize="16sp" />

        <TextView android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="2" android:background="@color/color_D6E519" android:paddingRight="15dp" android:text="这里5" android:textColor="@color/black" android:textSize="16sp" />
    </LinearLayout>
</LinearLayout>
复制代码

img

用代码方式编写linearlayout布局

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //建立LinearLayout布局对象
        LinearLayout liHello = new LinearLayout(this);
        //对于布局方面的属性这样来设置
        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        //设置布局LinearLayout的布局排列方式
        liHello.setOrientation(LinearLayout.VERTICAL);
        //设置布局背景颜色
        liHello.setBackgroundColor(Color.parseColor("#F9658E"));
        //设置布局内边距,注意这里不能够设置外边距
        liHello.setPadding(10, 20, 30, 40);
        //设置组件内所包含的子元素的对齐方式
        liHello.setGravity(Gravity.CENTER);

        TextView tvHello = new TextView(this);
        tvHello.setText("你好");

        liHello.addView(tvHello, param);
        //设置显示liHello布局
        setContentView(liHello);
    }
复制代码

img

结语

咱们的软件是由好多个界面组成的,而每一个界面又由N多个控件组成,Android中借助布局来让各个空间有条不紊的摆放在界面上。能够把布局看做是一个能够放置不少控件的容器,它能够按照必定的规律调整控件的位置,从而实现精美的界面。布局中也能够放置布局,经过多层布局的嵌套,实现比较复杂的界面。相信小伙伴儿们已经学会LinearLayout的使用方法了,那就赶忙操练起来吧。

PS:若是还有未看懂的小伙伴,欢迎加入咱们的QQ技术交流群:892271582,里面有各类大神回答小伙伴们遇到的问题哦~

相关文章
相关标签/搜索