2.1五种布局方式
1.LinearLayout线性布局
线性布局是以水平或垂直方式依次排列显示控件的布局方式。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮一"
android:textSize="20dp" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮二"
android:textSize="20dp" />
</LinearLayout>
2.RelativeLayout相对布局
相对布局是通过相对位置布局控件的布局方式,是Android采用的默认方式。在设计中相对布局遵循控件之间的依赖关系,后放入控件的位置依赖于先放入的控件,以其他控件或父容器为参照,摆放控件位置。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮一"
android:textSize="60dp" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮二"
android:textSize="10dp" />
</RelativeLayout>
3.FrameLayout帧布局
帧布局是Android中最简单的布局,该布局为每个加入其中的控件创建一个空白区域(称为帧),每个控件占据一帧。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="按钮一"
android:textSize="10dp" />
<Button
android:id="@+id/btn_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮二"
android:textSize="10dp" />
</FrameLayout>
4.TableLayout表格布局
表格布局是以表格的形式排列控件,通过行和列将界面划分为多个单元格,每个单元格都可以添加控件。
表格布局需要和TableRow配合使用,每一行都由TableRow对象组成,因此TableRow的数量决定表格的行数,而表格的列数由TableRow包含最大控件数决定的。
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:shrinkColumns="2"
tools:context=".MainActivity">
<!--LinearLayout线性布局、RelativeLayout相对布局、FrameLayout帧布局、表格布局、绝对布局-->
<!--android:layout_gravity用于指定控件在布局中的对齐方式-->
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="按钮2" />
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:text="按钮3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:text="按钮4" />
</TableRow>
</TableLayout>
表格布局的属性&表格控件的属性
- 布局属性
布局属性 | 功能描述 |
---|---|
Android:strechColumns | 设置该列被拉伸列号从“0”开始例如Android:strechColumns=“0”表示第一列拉伸 |
Android:shrinkColumns | 设置该列被收缩列号从“0”开始例如Android:shrinkColumns=“1,2”表示第2,3列可收缩 |
Android:CollapseColumns | 设置该列被隐藏列号从“0”开始例如Android:CollapseColumns=“1”表示第一1,2列隐藏 |
- 控件属性
控件属性 | 功能描述 |
---|---|
Android:layout_column | 设置该单元显示位置、例如,Android:layout_column=1表示在第二个位置显示 |
Android:layout_span | 设置该单元格占据几列,默认为1列 |
5.AbsoluteLayout绝对布局
绝对布局是通过指定的x,y坐标来控制每一个控件的位置。
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50dp"
android:layout_y="50dp"
android:text="按钮1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="200dp"
android:layout_y="150dp"
android:text="按钮2" />
</AbsoluteLayout>
2.2 常用控件
控件是界面组成的重要元素,TextView文本框,EditView编辑框,Button按钮等,这些都是与用户进行交互的控件。
2.2.1 TextView文本框
通过TextView控件显示文本信息,如我们例程中的“Hello World!”。
TextView常用属性
控件属性 | 功能描述 |
---|---|
android:text | 设置显示文本 |
android:textColor | 设置文本的颜色 |
android:textSize | 设置文字大小,推荐单位sp |
android:textStyle | 设置文字样式,如bold(粗体),italic(斜体) |
android:height/width | 设置文本区域的高度/宽度,支持单位px/dp |
androidlayout_width | 设置TextView控件的宽度 |
android:maxLength | 设置文本长度,超出不显示 |
android:password | 设置文本以密码形式“…”显示 |
android:gravity | 设置文本位置,如设置成“center” |
android:phoneNumber | 设置以电话号码的方式输入 |
android:layout_height | 设置TextView控件的高度 |
2.2.2 EditView编辑框
通过EditView控件接收用户输入信息,将用户信息传递给Android程序。
EditView常用属性
控件属性 | 功能描述 |
---|---|
android:hint | 设置EditText没有输入内容时显示的提示信息 |
android:llines | 设置固定行数来决定EditText的高度 |
android:maxLines | 设置最大行数 |
android:minLines | 设置最小行数 |
android:password | 设置文本以密码形式“…”显示 |
android:phoneNumber | 设置以电话号码的方式输入 |
android:scrollHorizontally | 设置文本超出TextView的宽度情乱下,是否出现横拉条 |
android:capitalize | 设置首字母大写 |
android:editable | 设置是否可编辑 |
练习
MainActivity.java
文件
public class MainActivity extends AppCompatActivity {
private Button myBtn_one;
private Button myBtn_two;
@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
//初始化
myBtn_one = (Button) findViewById(R.id.btn_one);
myBtn_two = (Button) findViewById(R.id.btn_two);
myBtn_two.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myBtn_two.setText("按钮2被点击");
}
});
}
public void click(View v) {
myBtn_one.setText("按钮1被点击");
}
}
activity_main.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="click"
android:text="按钮1" />
<Button
android:id="@+id/btn_two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/btn_one"
android:text="按钮2" />
</RelativeLayout>
运行结果