Android XML 布局基础(五)线性布局 - LinearLayout

简介: Android XML 布局基础(五)线性布局 - LinearLayout

一、简介

  • 多种 Layout 布局是可以嵌套组合使用的。
  • LinearLayout 是一个视图容器,用于使所有子视图在单个方向(垂直或水平)保持对齐,可使用 android:orientation 属性指定布局方向。
  • 测试代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- 由于最外层不是 Layout,则这里为根布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="#ffc">
        <!-- 子视图列表 -->
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#cff"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#fcf"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#cff"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#fcf"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#cff"/>
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

二、属性

  • android:orientation="horizontal"
    测试代码同上,修改一下方向代码即可。

  • android:orientation="vertical"
    测试代码同上,修改一下方向代码即可。

  • android:layout_weight="1"
    通过给子视图设置权重值,来分配子视图所占空间的权重(比例),如图三个子视图权重分别设置为 1,则均分页面空间。
<?xml version="1.0" encoding="utf-8"?>
 <androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity">
     <!-- 由于最外层不是 Layout,则这里为根布局 -->
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="horizontal"
         android:background="#ffc">
         <!-- 子视图列表 -->
         <View
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:layout_weight="1"
             android:background="#cff"/>
         <View
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:layout_weight="1"
             android:background="#fcf"/>
         <View
             android:layout_width="80dp"
             android:layout_height="80dp"
             android:layout_weight="1"
             android:background="#cff" />
     </LinearLayout>
 </androidx.constraintlayout.widget.ConstraintLayout>


  • 也可以组合使用
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <!-- 由于最外层不是 Layout,则这里为根布局 -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="#ffc">
        <!-- 子视图列表 -->
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:background="#cff"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_weight="2"
            android:background="#fcf"/>
        <View
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_weight="0.5"
            android:background="#cff" />
    </LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>


相关文章
|
7月前
|
存储 消息中间件 人工智能
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
226 11
【05】AI辅助编程完整的安卓二次商业实战-消息页面媒体对象(Media Object)布局实战调整-按钮样式调整实践-优雅草伊凡
|
7月前
|
XML 存储 Java
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
181 3
【06】AI辅助编程完整的安卓二次商业实战-背景布局变更增加背景-二开发现页面跳转逻辑-替换剩余图标-优雅草卓伊凡
|
11月前
|
Android开发 开发者
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
本文详细介绍了如何通过自定义 `attrs.xml` 文件实现 Android 自定义 View 的属性配置。以一个包含 TextView 和 ImageView 的 DemoView 为例,讲解了如何使用自定义属性动态改变文字内容和控制图片显示隐藏。同时,通过设置布尔值和点击事件,实现了图片状态的切换功能。代码中展示了如何在构造函数中解析自定义属性,并通过方法 `setSetting0n` 和 `setbackeguang` 实现功能逻辑的优化与封装。此示例帮助开发者更好地理解自定义 View 的开发流程与 attrs.xml 的实际应用。
298 2
Android自定义View之不得不知道的文件attrs.xml(自定义属性)
|
XML 编解码 搜索推荐
XML 布局小技巧
【10月更文挑战第24天】通过掌握这些 XML 布局小技巧,我们可以更轻松地设计出高质量的用户界面,提升用户体验。在实际应用中,要根据具体项目的需求和特点,灵活运用这些技巧,不断探索和创新,打造出独具特色的界面布局。
337 1
|
移动开发 监控 前端开发
构建高效Android应用:从优化布局到提升性能
【7月更文挑战第60天】在移动开发领域,一个流畅且响应迅速的应用程序是用户留存的关键。针对Android平台,开发者面临的挑战包括多样化的设备兼容性和性能优化。本文将深入探讨如何通过改进布局设计、内存管理和多线程处理来构建高效的Android应用。我们将剖析布局优化的细节,并讨论最新的Android性能提升策略,以帮助开发者创建更快速、更流畅的用户体验。
253 10
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
|
XML JavaScript Android开发
【Android】网络技术知识总结之WebView,HttpURLConnection,OKHttp,XML的pull解析方式
本文总结了Android中几种常用的网络技术,包括WebView、HttpURLConnection、OKHttp和XML的Pull解析方式。每种技术都有其独特的特点和适用场景。理解并熟练运用这些技术,可以帮助开发者构建高效、可靠的网络应用程序。通过示例代码和详细解释,本文为开发者提供了实用的参考和指导。
490 15
|
编解码 Android开发
【Android Studio】使用UI工具绘制,ConstraintLayout 限制性布局,快速上手
本文介绍了Android Studio中使用ConstraintLayout布局的方法,通过创建布局文件、设置控件约束等步骤,快速上手UI设计,并提供了一个TV Launcher界面布局的绘制示例。
509 1
|
存储 Java 数据库
基于全志H713 Android 11:给TvSettings添加default.xml默认值
本文介绍了在全志H713 Android 11平台上为TvSettings应用添加HDMI CEC功能的默认设置值的方法,通过修改SettingsProvider的源码和配置文件来实现默认值的设置,并提供了详细的步骤和测试结果。
796 0
基于全志H713 Android 11:给TvSettings添加default.xml默认值
|
ARouter Android开发
Android不同module布局文件重名被覆盖
Android不同module布局文件重名被覆盖
1067 0

热门文章

最新文章