【Android】界面布局-线性布局LinearLayout-例子

线性布局(LinearLayout)是一种重要的界面布局中,也是经常使用到的一种界面布局

• 在线性布局中,所有的子元素都按照垂直或水平的顺序在界面上排列

➢如果垂直排列,则每行仅包含一个界面元素

➢如果水平排列,则每列仅包含一个界面元素

代码示例

用户名-请输入用户名-确认-取消

1.水平实现

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名" />

    <EditText
        android:id="@+id/editTextText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:minHeight="48dp"
        android:text="请输入用户名" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="确认" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="取消" />

</LinearLayout>

结果演示 :

2.垂直实现

仅需修改参数为vertical

XML 复制代码
android:orientation="vertical"

结果演示 :

Tips

直接拖动,创建所需组件

上面创建的就是输入框EditText:

  • 功能:允许用户输入文本,常用于表单数据收集。
  • 关键属性
    • android:inputType:定义输入类型(如 textnumberemail)。
    • android:hint:设置输入框的提示文本(用户输入时消失)。
相关推荐
针叶4 小时前
Google Play加固保护导致的崩溃
android·安全·google
执明wa6 小时前
Android Studio 项目目录结构全方位详解
android·ide·android studio
__Witheart__7 小时前
Android编译错误:Soong阶段因缺失res目录导致panic (Iwlan模块)
android
酿情师8 小时前
逆向exe文件:CRT 初始化流程详细分析
android·软件构建·逆向·re·crt‘
问心无愧051310 小时前
ctf show web入门71
android·前端·笔记
夜勤月10 小时前
AQS 与 ThreadPoolExecutor 深度拆解:JDK 高并发底层设计精髓
android·java·开发语言
TZ丶旭哥10 小时前
Typora+Gitee+PicGo图床设置
gitee·typora·picgo
Yeyu10 小时前
Android 卡顿诊断 SDK:从痛点出发的设计思考
android
上天_去_做颗惺星 EVE_BLUE11 小时前
Ubuntu Android 虚拟机安装使用教程
android·linux·测试工具·ubuntu·安卓
我命由我1234511 小时前
Android 开发问题:Could not find com.github.PicnicSupermarket:FingerPaintView:1.2.
android·github·android studio·安卓·android jetpack·android-studio·android runtime