【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:设置输入框的提示文本(用户输入时消失)。
相关推荐
yige455 分钟前
【MySQL】MySQL内置函数--日期函数字符串函数数学函数其他相关函数
android·mysql·adb
洞见前行20 分钟前
AI 当逆向工程师:Claude Code 自主分析 APK 和 so 文件,解决 Unity 插件化启动崩溃
android·人工智能
努力进修41 分钟前
旧安卓手机别扔!用KSWEB搭个人博客,搭配外网访问超香
android·智能手机·cpolar
范特西林1 小时前
一文看懂Android SELinux 策略,从“拒绝”到“允许”的距离
android
是翔仔呐2 小时前
第13章 SPI通信协议全解:底层时序、4种工作模式与W25Qxx Flash芯片读写实战
c语言·开发语言·stm32·单片机·嵌入式硬件·学习·gitee
是翔仔呐3 小时前
第14章 CAN总线通信全解:底层原理、帧结构与双机CAN通信实战
c语言·开发语言·stm32·单片机·嵌入式硬件·学习·gitee
客卿1233 小时前
用两个栈实现队列
android·java·开发语言
studyForMokey3 小时前
【Android面试】Gradle专题
android·面试·职场和发展
coderYYY3 小时前
git push报错Authentication failed for ‘xxx’也不会弹要求输入用户名密码的最终解决方法
前端·git·gitee·github
向上_503582915 小时前
配置Protobuf输出Java文件或kotlin文件
android·java·开发语言·kotlin