Android基础(二)--Android五大布局

Android基础知识整理第二篇


Android的布局

  • 布局分为以下五种:

    • RelativeLayout
    • LinearLayout
    • GridLayout
    • FrameLayout
    • AbsoluteLayout

这里需要解释一下,什么是布局。在Android中,关于UI的设计,很多时候是基于一种特定的页面内容的分布方式来进行的,简单的说,就是我的一个按钮和另外一个按钮,一个在上面,一个在下面,这就是一种布局的方式。
  1. RelativeLayout - 相对布局

首先我们看一段代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.csdn.myapplication.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The First TextView" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The Second TextView" />
</RelativeLayout>
  • 这段代码中,可以看到代码最外层是一个<RelativeLayout></RelativeLayout>在里面放了两个TextView(这里先知道TextView是显示文字的控件,之后再进行详解),然后我们看一下运行起来是什么样子的。

这里写图片描述

  • 很显然,两个文字重叠在了一起,什么也看不清,既然是相对布局,我们要去实现他们的相对性,就要靠一些代码来完成了。
  • 相对布局的作用,就是让在这个布局内的控件可以互相作为位置的参照。

我们修改一下代码中的一些内容

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.csdn.myapplication.MainActivity">

    <TextView
        android:id="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The First TextView" />
    <TextView
        android:id="@+id/tv_2"
        android:layout_below="@+id/tv_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="The Second TextView" />
</RelativeLayout>
  • 我们可以看到,在每个TextView中,加入了一行代码,android:id="@+id/" 这里,我们可以为任何一个控件(最外层的RelativeLayout也可以设置id)设置一个id,这个id就作为了这个控件的标识。

  • 另外一行代码android:layout_below="@+id/tv_1",这里表示当前控件在id为tv_1的下面(below)。我们看一下效果。

  • 可以看到,我们的第二个控件已经在第一个控件的下面了。

这里写图片描述


  • 我们从上面的简单示例中理解一个问题,就是每一个控件都有他们的属性,表示一定的含义,来控制这个控件表现出来的样式,位置或者其他特性。

  • 那么,不光是TextView拥有这些属性,我们称TextView外层嵌套的RelativeLayout为父布局控件 (这里我们把RelativeLayout也称为一个控件,实际上,所有的控件都是继承至View类或者ViewGroup这个类,这个将在以后整理中慢慢进行理解)

  • RelativeLayout也拥有一些属性。既然所有的控件都有很多属性,这些属性是不是会有一些属性是通用的呢,答案是肯定的,列一个表格来看一下一些常用的属性。

属性含义
android:id@+id/tv_1为控件社会自id标志
android:background可以是颜色的值(#ffffff),背景图片,或者背景文件设置控件的颜色,背影图片
android:gravitytop、bottom、left、right、center…设置这个控件中内容显示的对齐方式
android:padding例:10dp控件的内边距
android:margin例:10dp控件的外边距
android:onClick在activity类中绑定的点击方法方法名设置这个方法的点击事件
android:clickabletrue 或者 false设置这个控件是否可以点击
android:layout_height例:10dp或wrap_content或match_parent为控件设置高度
android:layout_width例:10dp或wrap_content或match_parent为控件设置宽度
  • 以上的这些属性,是非常基础的,也是平时使用非常频繁的,其中,有几个属性说一下,backgroundpadding,marginonClick

  • 前两个要放在一起解释,前面的是内边距,后面的是外边距,可以从下面的图看,整个控件是深色的方框,margin 就是在这个控件外面加边距,而padding则是以控件的边框为基准,向内添加边距,使得控件中的内容会距离边框更远。

  • 属性的值的话,有三种方式,10dp表示10个像素表示除了文字意外的尺寸,match_parent表示填充满父布局,上面的例子的话,如果TextView使用这个属性值,会填满RelativeLayout。wrap_content表示适配当前控件内容的高度或者宽度,文字有多大,控件就有多高多宽。

这里写图片描述
注:图片来自网络

  • onClick这个属性的话,我们会在整理button控件的时候第一次用到,这里不做解释。

  • background这个属性,可以为它添加颜色,也可以添加背景图片。

    • 颜色: #ffffff代表白色,6位十六进制数值表示颜色。
    • 如果为8位十六进制数值,前两位代表透明度ff代表不透明,00代表完全透明。
    • 背景图片:@drawable/图片名称 或者 @mipmap/图片名称。注:drawable和mipmap都是存放图片的资源文件夹

除了这些通用的属性以外,我们来看一下RelativeLayout内的子控件都有哪些属性。
属性含义
layout_alignParentRight该控件与父布局控件右对齐true or false
layout_alignParentLeft该控件与父布局控件左对齐true or false
layout_alignParentTop该控件与父布局控件顶端对齐true or false
layout_alignParentBottom该控件与父布局控件底部对齐true or false
layout_centerInParent该控件位于父布局控件中心位置true or false
layout_centerVertical该控件位于父布局控件垂直中心位置true or false
layout_centerHorizontal该控件位于父布局控件水平中心位置true or false
layout_toRightOf该控件在哪个控件的右侧另外一个子控件的id
layout_toLeftOf该控件在哪个控件的左侧另外一个子控件的id
layout_above该控件在哪个控件的上侧另外一个子控件的id
layout_below该控件在哪个控件的下侧另外一个子控件的id
layout_alignRight该控件与哪个控件的右对齐另外一个子控件的id
layout_alignLeft该控件与哪个控件的左对齐另外一个子控件的id
layout_alignTop该控件与哪个控件的顶对齐另外一个子控件的id
layout_alignBottom该控件与哪个控件的底对齐另外一个子控件的id

2.LinearLayout - 线性布局

我们看一下把布局改成线性布局以后的效果

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.csdn.myapplication.MainActivity">

    <TextView
        android:id="@+id/tv_1"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="The First TextView" />

    <TextView
        android:id="@+id/tv_2"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#9b9cff"
        android:text="The Second TextView" />
</LinearLayout>
  • 这段代码中,我们修改了几个地方,来看一下效果

这里写图片描述

  • 可以看到,布局中,两个控件是从左向右排列的,默认情况下是这样的。

  • 当我们在代码中加入一条属性,修改如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.csdn.myapplication.MainActivity">

这时候内部的控件就会竖直排列。如果需要设置横向排列的修改代码为android:orientation="horizontal"

LinearLayout特有的属性
属性含义
android:orientation控制布局是横向或者纵向vertical or horizontal
android:weightSum设置布局的权重综合,子控件可以设置权重来显示占父布局的比例数值 例:10
LinearLayout子控件特有的属性
属性含义
android:layout_weight子元素在LinearLayout中所占的权重数值 例:5
android:layout_gravity子元素在 LinearLayout 中的对齐方式center,top,bottom…

3.GridLayout - 网格布局

  • GridLayout我们首先还是来看一下效果
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.csdn.myapplication.MainActivity">

    <TextView
        android:id="@+id/tv_1"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff0000"
        android:text="The First TextView" />

    <TextView
        android:id="@+id/tv_2"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#9b9cff"
        android:text="The Second TextView" />

    <TextView
        android:id="@+id/tv_3"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#9efc8b"
        android:text="The Three TextView" />

    <TextView
        android:id="@+id/tv_4"
        android:padding="10dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ffa86e"
        android:text="The Four TextView" />
</GridLayout>
  • 我们在一个GridLayout中添加了4个TextView,看一下效果

这里写图片描述

  • 可以看到,我们屏幕上只显示了一行,三个TextView,而且最右边还没显示全,这是因为,GridLayout默认情况下,是一行多列的,在屏幕外面,还有我们的第四个TextView

  • 我们在GridLayout标签中,加入一条android:columnCount = "2"

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:columnCount = "2"
    tools:context="com.csdn.myapplication.MainActivity">
  • 这时候的效果如下图

这里写图片描述

  • 变成了两列,我们来归纳一下GridLayout的属性
GridLayout特有的属性
属性含义
android:columnCountGridLayout的最大列数数值 例:2
android:rowCountGridLayout的最大行数数值 例:2
android:orientationGridLayout中子元素的布局方向vertical horizontal
GridLayout子控件特有的属性
属性含义
android:layout_column在第几列显示当前子控件数值 例:2
android:layout_row在第几行显示当前子控件数值 例:2
android:layout_columnSpan列合并,表示当前控件占几列的大小数值 例:2
android:layout_rowSpan行合并,表示当前控件占几行的大小数值 例:2
android:layout_gravity当前控件在单元格内的布局方式center,top,bottom…

4.FrameLayout - 帧布局

  • FrameLayout布局比较简单,当在布局中添加控件时,新添加的控件会覆盖前面的控件。

  • 这种布局方式像是没有进行任何位置控制的RelativeLayout。


5.AbsoluteLayout- 绝对布局

  • 绝对布局是一种基于坐标的布局方式,布局缺少灵活性和适配性,目前版本已经弃用,不在做整理。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值