Android基础知识回顾-Android布局

Android中有六种基本布局:LinearLayout(线性布局)、TableLayout(表格布局)、RelativeLayout(相对布局)、FrameLayout(帧布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局)。其中常用的有LinearLayout(线性布局)、RelativeLayout(相对布局),而AbsoluteLayout(绝对布局)基本被弃用,这次主要就LinearLayout(线性布局)、RelativeLayout(相对布局)来说。

LinearLayout(线性布局)

相信布局通俗来讲是将控件按照一行一行或者一列一列排下去,不会换行,后面的超过屏幕宽度的不会显示。

线性布局分为垂直线性布局和水平线性布局,属性分别是

垂直线性布局

<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"
    android:orientation= "vertical"

水平线性布局

<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"
    android:orientation= "horizontal"

默认为horizontal(垂直线性布局)
重要属性:
android:orientation,当设置成 vertical 时表示布局容器内的控件纵向排列成1列,当设置成 horizontal 时表示布局容器内的所有控件横向排列成1行

android:layout_weight,为容器内的组件设置权重,表示当所有控件全部排列完毕后,该被设置的组件占父容器剩余空白部分的多少比重。需要注意的是:layout_weight只能在LinearLayout(线性布局)中使用,而且只能在LinearLayout中的直接子元素中使用。

android:layout_width:设置LinearLayout 的宽度。

android:layout_height:设置LinearLayout 的高度。

RelativeLayout(相对布局)

相对布局是以组件与组件之间的位置,组件与父容器之间的位置关系之间来排列各种组件的。

根据父容器来定位:

左对齐:android:layout_alighParentLeft=”true”
右对齐:android:layout_alighParentRight=”true”
顶端对齐:android:layout_alighParentTop=”true”
底部对齐:android:layout_alighParentBottom=”true”
水平居中:android:layout_centerHorizontal=”true”
垂直居中:android:layout_centerVertical=”true”
中央位置:android:layout_centerInParent=”true”

根据兄弟组件来定位:

左边:android:layout_toLeftOf=”兄弟组件id”
右边:android:layout_toRightOf=”兄弟组件id”
上方:android:layout_above=”兄弟组件id”
下方:android:layout_below=”兄弟组件id”
对齐上边界:android:layout_alignTop=”兄弟组件id”
对齐下边界:android:layout_alignBottom=”兄弟组件id”
对齐左边界:android:layout_alignLeft=”兄弟组件id”
对齐右边界:android:layout_alignRight=”兄弟组件id”

margin、padding属性

margin:设置组件与父容器的间距(外边距)

android:layout_margin: 指定控件的四周的外部留出一定的边距
android:layout_marginLeft: 指定控件的左边的外部留出一定的边距
android:layout_marginTop: 指定控件的上边的外部留出一定的边距
android:layout_marginRight: 指定控件的右边的外部留出一定的边距
android:layout_marginBottom: 指定控件的下边的外部留出一定的边距

padding:设置组件中的元素与组件内部边框之间的距离(内边距)

android:padding :指定控件的四周的内部留出一定的边距
android:paddingLeft: 指定控件的左边的内部留出一定的边距
android:paddingTop: 指定控件的上边的内部留出一定的边距
android:paddingRight: 指定控件的右边的内部留出一定的边距
android:paddingBottom: 指定控件的下边的内部留出一定的边距

猜你喜欢

转载自blog.csdn.net/s15770702468/article/details/80565049
今日推荐