LinearLayout 线性布局
一、开篇引言
在 Android 应用开发中,界面布局是构建用户交互界面的基础。Android 提供了两种实现界面布局的方式:一种是XML 布局,另一种是Java 代码布局。
XML 布局是将界面写在 .xml 格式的布局文件中,由系统负责解析和渲染,它的特点是界面与逻辑分离、结构清晰、支持可视化预览、便于屏幕适配,是 Android 官方推荐、项目中最常用的布局方式。
Java 代码布局则是在 Java 文件中通过代码动态创建控件、设置布局参数,不写 xml,完全在 Java 代码里创建控件、设置布局。它的优势是动态性强,可以根据程序逻辑随时修改界面。 例如:
LinearLayout layout = new LinearLayout(this);
Button btn = new Button(this);
btn.setText("按钮");
layout.addView(btn);
setContentView(layout);
本项目使用的是 XML 布局 实现界面,它能让我们更直观地学习 Android 四大常用布局的特性。本文将以 LinearLayout 项目为例,详细讲解线性布局的使用方法与核心属性。
二、项目运行效果
本项目通过 LinearLayout 实现水平方向的按钮排列,并利用权重属性完成按钮宽度的比例分配,运行效果如下:
界面呈现三个水平排列的按钮,按钮 3 的宽度为按钮 1、按钮 2 的两倍,完美体现了线性布局权重分配的核心特性。
三、核心代码解析
本项目为标准 Android 基础工程,仅需三个核心文件即可完成完整功能实现,无需额外代码文件,所有文件各司其职,构成了 Android 应用的基础架构。
1. 布局文件:activity_main.xml(最重要)
该文件是项目的核心界面文件,采用 XML 语法编写 LinearLayout 布局结构,定义了布局方向与子控件的属性、权重,是本次学习的重点。
<?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="horizontal">
<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="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="按钮3"/>
</LinearLayout>
-
android:orientation="horizontal":设置线性布局为水平方向,子控件从左至右依次排列;若设置为vertical,则为垂直排列。 -
android:layout_width="0dp":配合权重使用,将宽度交由layout_weight分配。 -
android:layout_weight:权重属性,用于按比例分配布局的剩余空间。本案例总权重为1+1+2=4,按钮 1、按钮 2 各占 1/4 宽度,按钮 3 占 2/4(1/2)宽度。 -
match_parent:表示控件尺寸填充父容器; -
wrap_content:表示控件尺寸自适应自身内容。
2.界面逻辑文件:MainActivity.java
Activity 是 Android 应用的界面载体,该文件的核心作用是加载 XML 布局文件,将布局与界面绑定,完成视图渲染。
package cn.edu.linearlayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 加载布局文件,将XML界面展示在当前Activity中
setContentView(R.layout.activity_main);
}
}
setContentView(R.layout.activity_main) 是核心代码,用于关联 XML 布局文件,是连接 Java 逻辑与 XML 视图的桥梁。XML 负责界面长什么样,Activity 负责把界面显示出来。
3. 应用配置文件:AndroidManifest.xml
该文件是 Android 应用的全局配置清单,用于声明应用包名、组件、权限、启动入口等核心信息,是应用运行的必备配置。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.linearlayout">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="cn.edu.linearlayout.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
通过 <intent-filter> 标签将 MainActivity 声明为应用启动入口,用户点击应用图标 → 系统找到这个配置 → 启动 MainActivity → 显示界面。
LinearLayout 是 Android 四大基本布局之一,属于线性布局容器,子控件仅支持 水平(horizontal)或 垂直(vertical) 单方向排列。 理解了 LinearLayout 的核心属性与使用方式,同时明确了 Android 项目中三大核心文件的作用:
四、总结
掌握Android 中 XML 布局与 Java 代码布局的区别,理解 LinearLayout 的核心属性与使用方式,同时明确了 Android 项目中三大核心文件的作用:
- ✅ XML 布局文件:负责界面结构
- ✅ MainActivity:负责界面加载与逻辑
- ✅ AndroidManifest:负责应用配置
三者协同工作,构成了 Android 应用的基础架构