Android提供了许多布局空间,对于不同的格式需求选择不同的布局控件,可以大大简化代码操作。下面为大家介绍一下以下经常用到的五种布局。
对了,补充一下,往常Android只有约束布局可以直接在Design页面拖动,现在都可以了。不过不推荐。
1.linear_layout(线性布局)
线性布局正如其名,他是呈线性分布的布局。只能控制水平或者垂直方向排布控件。可以用于设置登陆界面等简单排布的界面,下面有颜色填充一个线性布局,大家可以参考一下:
重点在于这句:android:orientation=“vertical” 在这个布局中控件垂直分布
android:orientation=“horizontal” 在这个布局中控件水平分布
如果复杂一些,大家灵活运用,各种嵌套肯定没问题。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="10dp"> //设置左内边距
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第一行"
android:textColor="#ffffff"
android:background="#550000"
android:layout_weight="1"
android:gravity="center_vertical" //设置控件位置
android:paddingLeft="20dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第二行"
android:textColor="#ffffff"
android:background="#005500"
android:layout_weight="1"
android:gravity="center_horizontal|center_vertical"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="第三行"
android:textColor="#ffffff"
android:background="#000055"
android:layout_weight="1"
android:gravity="right|center_vertical&#