Android六大基本布局分别是:线性布局LinearLayout、表格布局TableLayout、相对布局RelativeLayout、层布局FrameLayout、绝对布局AbsoluteLayout、网格布局GridLayout。 在android开发中,用的相对频繁的是线性布局和相对布局,在大多数的情况下使用这两种布局都能完成。

一、线性布局LinearLayout最常用的属性有:

  • android:id :定义布局id,即标识符,可以通过id来找到该布局或者控件
  • android :layout_width :布局宽度,有match_parent ,wrap_content,fill_paren
  • android:layout_height :布局高度,有match_parent,wrap_content,fill_paren
  • android:background :设置布局的背景,可以用颜色,也可以使用图片,颜色常以六位的十六进制表示
  • android:layout_margin :外边距,布局或控件距离外部元素的边距
  • android:layout_padding :内边距,布局或控件距离内部元素的边距
  • android:orientation :布局方向,水平布局horizontal,垂直布局vertical
  • android:layout_weight:权重,除了被显示占据的空间以外的的空间,然后根据权重的大小来分配空间,使用权重通常会把分配该权                                     重方向的宽度设置为0dp,如果未设置0dp,则该控件会占据指定的宽度,然后再加上根据权重来分配的空间

二、相关的例子:

首先创建一个空间,宽度为200dp,高度为200dp,背景颜色为黑色,android:id="@+id/ll_1"为为这个空间取名为ll_1

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main"
    android:background="@drawable/ic_launcher_background"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="#090909">
    </LinearLayout>

</LinearLayout>

对应的界面如下:

LinearLayout android 局下 安卓linearlayout_android开发

然后再内部将该空间平分:在使用布局时,在父类ll_1布局中加入android:orientation="vertical",使得空间内的其他布局或者组件垂直排列。方法一使用明确的高度来均分空间,方法二使用权重来分配空间,因为为垂直排列,所以高度设置为0dp。需要说明一下,之所以使用dp来做单位,是为了适应手机的不同分辨率。

第一个空间ll_2使用了用图片作为背景的方法,android:id="@+id/ll_1"为取id名为ll_1,而android:id="@drawable/img01"为引用drawable目录下的名字为img01的图片

<?xml version="1.0" encoding="utf-8"?>
<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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".MainActivity"
    tools:showIn="@layout/activity_main"
    android:background="@drawable/ic_launcher_background"
    android:orientation="vertical">
    <LinearLayout
        android:id="@+id/ll_1"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:orientation="vertical"
        android:background="#090909">
        <!--方法一-->
        <!--<LinearLayout-->
            <!--android:id="@+id/ll_2"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="100dp"-->
            <!--android:background="#ffffff">-->

        <!--</LinearLayout>-->
        <!--<LinearLayout-->
            <!--android:id="@+id/ll_3"-->
            <!--android:layout_width="match_parent"-->
            <!--android:layout_height="100dp"-->
            <!--android:background="#FFEC8B">-->

        <!--</LinearLayout>-->
        <!--方法二,使用权重的方法-->
        <LinearLayout
            android:id="@+id/ll_2"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:background="@drawable/img01"></LinearLayout>
        <LinearLayout
            android:id="@+id/ll_3"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:orientation="vertical"
            android:layout_weight="1">

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

LinearLayout android 局下 安卓linearlayout_android开发_02

如果将方法二中的ll_3空间的高度设置为android:layout_height="100dp",那么此空间会首先占据100dp的高度,然后在300dp的高度中均分,所以结果为:

LinearLayout android 局下 安卓linearlayout_xml_03

然后是内边距和外边距,简单来说内边距(padding)就是父类的容器里面的元素距离父类边界的距离,而外边距(margin)则是两个容器之间的距离。

<LinearLayout
            android:id="@+id/ll_3"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_weight="1"
            android:orientation="vertical"
            android:padding="10dp ">
               <TextView
                    android:id="@+id/tx1"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:background="#FFB90F"
                    android:text="tx1"
                    />
                <TextView
                    android:id="@+id/tx2"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:background="#FAFAD2"
                    android:layout_marginTop="10dp"
                    android:text="tx2"/>
        </LinearLayout>

LinearLayout android 局下 安卓linearlayout_xml_04

可以看到tx1容器相距父容器ll_3的顶部和右部有10dp的宽度。而tx2因为设置了android:layout_marginTop="10dp",所以与tx2容器有间隔为10dp的距离。显而,如果改变tx2的高度为200dp,tx2的高度也不会改变,始终会与ll_3的底部有10dp的距离,这是由于ll_3容器中设置了android:padding="10dp "。使得ll_3容器内的元素距离它的边界都有10dp的距离。

padding可以用margin来替换,比如在ll_3中设置了android:paddingTop="10dp ",可以使tx2的顶部距离ll_3容器的顶部10dp,也可以在tx1中设置  android:layout_marginTop="10dp",使得tx1容器与外边距离l_3容器10dp。

①设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。

②设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。

③Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了 

所以,多加练习就能记住LinearLayout的属性。并且这些属性在很多布局都适用,除了android:orientation属性。

 

终于写完了第一篇博客,٩(๑>◡<๑)۶!!!!!