Button android studio 与其他空间之间的距离 android studio 相对布局_android

一、Android五大布局分类

1、相对布局


2、绝对布局


3、线性布局


4、表格布局


5、帧布局




二、具体布局的使用(这里主要介绍相对布局和线性布局)


1、相对布局(RelativeLayout)


可以这样理解:在安卓屏幕中的父元素就是整个屏幕,而子元素就是那些按钮、文本框之类的东西。相对布局内视图可用的常用属性如下:


与位置相关的属性


android:layout_above 表示在目标组件之上


android:layout_below 表示在目标组件之下


android:layout_toLeftOf 表示在目标组件的左边


android:layout_toRightOf 表示在目标组件的右边与对齐相关的属性


android:alignBaseLine 表示与目标组件的基线对齐


android:alignBottom 表示与目标组件的底边对齐


android:alignTop 表示与目标组件的顶边对齐


android:alignLeft 表示与目标组件的左边对齐


android:alignRight 表示与目标组件的右边对齐


android:layout_centerHorizontal 表示在相对布局容器内水平居中


android:layout_centerVertical 表示在相对布局容器内垂直居中




3、线性布局(LinearLayout)


线性布局是一种让视图水平或垂直线性排列的布局,又分为横向和纵向。线性布局按照垂直或者水平的顺序依次排列子元素,每一个子元素都位于前一个元素之后。


1)垂直排列,那么将是一个N行单列的结构,每一行只会有一个元素,而不论这个元素的宽度为多少;


2)水平排列,那么将是一个单行N列的结构。


3)搭建两行两列的结构,通常的方式是先垂直排列两个元素,每一个元素里再包含一个LinearLayout进行水平排列




线性布局的常用属性如下:


android:orientation 表示布局方向


取值vertical表示垂直布局


取值horizontal表示水平布局


android:gravity 表示视图的对齐方式


内容包括:top、bottom、left、right、center_vertical、center_horizontal、center


可以使用|分隔填写多个值


布局中的视图可以使用如下属性


android:layout_gravity 表示单个视图的对齐方式


android:layout_weight 表示单个视图所在大小的比重


当layout_weight为0时,视图大小由自身确定


当layout_weight大于0时,视图在线性布局方向上根据比重拉伸





布局中常用到的关键字:


match_content 包含内容


match_parent 匹配父控件


padding 内边距


margin 外边距


weight 权重 用来分父控件的空间

下面是XML文件Hello World代码,就是一个简单的TextView

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

    <TextView
        android:id="@+id/L1_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hellow World!"
        android:textSize="30sp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textColor="#ff0000"/>

</RelativeLayout>