【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:设置输入框的提示文本(用户输入时消失)。
相关推荐
louisgeek5 分钟前
Android NSD 网络服务发现
android
张可1 小时前
历时两年半开发,Fread 项目现在决定开源,基于 Kotlin Multiplatform 和 Compose Multiplatform 实现
android·前端·kotlin
余辉zmh1 小时前
【Linux系统篇】:信号的生命周期---从触发到保存与捕捉的底层逻辑
android·java·linux
孤鸿玉2 小时前
[Flutter小试牛刀] 低配版signals,添加多层监听链
android·前端·响应式设计
雨和卡布奇诺2 小时前
LiveData源码浅析
android
淡蓝色_justin2 小时前
Hilt-plus 简介
android·android jetpack
app1e2342 小时前
ctfshow web入门 命令执行(29-77)
android·前端
恋猫de小郭4 小时前
Flutter 在 Dart 3.8 开始支持 Null-Aware Elements 语法,自动识别集合里的空元素
android·前端·flutter
fatiaozhang95275 小时前
咪咕MG101_晨星MSO9380芯片_安卓5.1.1_免拆卡刷固件包
android·电视盒子·av1·机顶盒rom·魔百盒刷机