Android 布局学习
Youtube 学习记录。
主要是根据 Youtube 的一个安卓 Youtuber 的视频教程来学习,或者说复习一下安卓的布局。以前看过《Android 第一行代码》第二版。
LinearLayout
线性布局。
<?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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="First Name"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Last Name"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Birth Date" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Country" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Apply" />
</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"一般来说,这三个命名空间是必须的。
我们设置 layout_width 的值常用的有三种:
- match-parent: 最外面的
LinearLayout如果使用这个值,会填充整个屏幕。 - wrap-content: 包裹内容的大小即可。
- 20dp: 20 仅仅是一个示例,实际可以设置成任何值。
orientation 是放置的方式。分为水平和垂直两种方式。
在 LinearLayout 布局中可以再嵌套
LinearLayout 布局。

ConstraintLayout
首先,熟悉一下 Android Studio 的使用。

创建三个按钮,并让它们水平和垂直居中:


然后依然是一些关于调整位置方面的东西。
- chains
- baseline
利用纯拖拽创建的一个页面:


Rename 快捷键:Shift + F6
Android 布局学习
http://fanyfull.github.io/2021/12/14/Android-布局学习/