-
什么是布局
就是把界面中的控件按照某种规律摆放到指定的位置 -
布局的二种实现
代码
xml配置文件:res/layout目录下
注:也可以同时使用xml和代码 -
布局的基本属性
设置背景颜色 android:background="@color/green"
设置内间距 android:padding=“50dp”
设置外间距 android:layout_margin=“50dp”
设置布置方式: vertical垂直布置 horizontal水平布置
android:orientation=“vertical”
设置位置
多个位置用|区分 android:gravity=“center” 匹配父类的屏幕大小 match_parent
<?xml version="1.0" encoding="utf-8"?>
<!--
设置背景颜色
android:background="@color/green"
设置内间距
android:padding="50dp"
设置外间距
android:layout_margin="50dp"-->
<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:background="@color/green"
android:padding="50dp"
android:layout_margin="50dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello World!"
android:background="@color/red"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</LinearLayout>
android:gravity和android:layout_gravity
用一个TextView、文字、背景查看效果最方便
android:gravity:控件内部的元素
android:layout_gravity:控件所在父元素的位置
但父元素的水平和垂直设置的优先度更高
4、 常见布局
线性布局(重点) LinearLayout
表格布局(几乎不用) TableLayout
帧布局 FrameLayout(就好象一张张卡片堆叠上去,后面会盖出前面的
绝对布局 AbsoluteLayout
相对布局 RelativeLayout
网格布局 GridLayout
RTL(几乎不用)
5、LinearLayout布局中Layout_weight属性的作用与误区
android:layout_weight 权重
误区:
layout_weight理解成将activity整个手机屏幕按比例区划分了,理解成反比了
正确理解
layout_weight划分的不是整个手机屏幕,划分的是屏幕剩余空件