【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:设置输入框的提示文本(用户输入时消失)。
相关推荐
田一一一3 小时前
Android framework 中间件开发(三)
android·中间件·framework·jni
不念霉运5 小时前
Gitee DevOps:中国企业数字化转型的“本土化加速器“
运维·gitee·团队开发·代码规范·devops·代码复审
androidwork8 小时前
掌握 Kotlin Android 单元测试:MockK 框架深度实践指南
android·kotlin
田一一一8 小时前
Android framework 中间件开发(二)
android·中间件·framework
追随远方8 小时前
FFmpeg在Android开发中的核心价值是什么?
android·ffmpeg
神探阿航9 小时前
HNUST湖南科技大学-安卓Android期中复习
android·安卓·hnust
千里马-horse11 小时前
android vlc播放rtsp
android·media·rtsp·mediaplayer·vlc
難釋懷11 小时前
Android开发-文本输入
android·gitee
志存高远6613 小时前
(面试)Android各版本新特性
android
IT从业者张某某13 小时前
信奥赛-刷题笔记-队列篇-T3-P3662Why Did the Cow Cross the Road II S
android·笔记