【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:设置输入框的提示文本(用户输入时消失)。
相关推荐
androidwork8 小时前
Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
android·java·kotlin·androidx
每次的天空8 小时前
Android第十三次面试总结基础
android·面试·职场和发展
wu_android8 小时前
Android 相对布局管理器(RelativeLayout)
android
李斯维10 小时前
循序渐进 Android Binder(二):传递自定义对象和 AIDL 回调
android·java·android studio
androidwork10 小时前
OkHttp 3.0源码解析:从设计理念到核心实现
android·java·okhttp·kotlin
像风一样自由11 小时前
【001】frida API分类 总览
android·frida
casual_clover11 小时前
Android 之 kotlin 语言学习笔记四(Android KTX)
android·学习·kotlin
移动开发者1号12 小时前
Android 大文件分块上传实战:突破表单数据限制的完整方案
android·java·kotlin
移动开发者1号12 小时前
单线程模型中消息机制解析
android·kotlin
每次的天空15 小时前
Android第十五次面试总结(第三方组件和adb命令)
android