Android的页面布局小结
关于布局
Android程序中界面是通过布局文件设置的,在每个应用程序创建时会默认包含一个主界面布局,该布局位于res/layout中。由于实际开发中每个应用程序都包含多个界面,而程序默认提供的一个主界面布局无法满足需求,因此经常会在程序中添加多个布局。
接下来将逐个回顾所学习的布局
布局举例
LinearLayout—线性布局
在实际开发中比较常用的布局,它主要以水平和垂直的方式来显示界面中的控件。在线性布局中,线性布局管理器允许为每一个子视图指定一个weight属性,以控制每一个子视图在空间内的大小。而线性布局中的orientation属性则是用于控制控件的排列方向。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
</LinearLayout>
如果将orientation的vertical设置成horizontal则会变成水平排列,如下图显示
可以使用layout_weight属性来设置每行控件的相对大小
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="按钮2"/>
<Button
android:layout_width=