Android五种布局方式+常用控件

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>

运行结果

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值