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>


相关文章
|
4月前
|
Android开发
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
Android Studio入门之常用布局的讲解以及实战(附源码 超详细必看)(包括线性布局、权重布局、相对布局、网格布局、滚动视图 )
137 0
|
4月前
|
Android开发 容器
Android开发,学习LinearLayout布局
Android开发,学习LinearLayout布局
39 0
|
4月前
|
XML Java Android开发
Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)
Android Studio App开发之循环试图RecyclerView,布局管理器LayoutManager、动态更新循环视图讲解及实战(附源码)
46 0
|
4月前
|
XML Java Android开发
Android Studio App开发中工具栏Toolbar、溢出菜单OverflowMenu、标签布局TabLayout的讲解及实战(实现京东App的标签导航栏,附源码)
Android Studio App开发中工具栏Toolbar、溢出菜单OverflowMenu、标签布局TabLayout的讲解及实战(实现京东App的标签导航栏,附源码)
63 0
|
4月前
|
Android开发 容器
Android开发第二次课 布局方式
Android开发第二次课 布局方式
24 0
|
6月前
|
XML 前端开发 Android开发
android 前端常用布局文件升级总结(一)
android 前端常用布局文件升级总结(一)
|
8月前
|
XML Java Android开发
#4,Android Studio Android程序结构 工程目录介绍 文件作用 运行配置文件AndroidManifest.xml
#4,Android Studio Android程序结构 工程目录介绍 文件作用 运行配置文件AndroidManifest.xml
|
8月前
|
Android开发
Android 使用DataBinding时 将布局页面转换为数据绑定布局(Convert to data binding layout) 不出现提示解决办法
Android 使用DataBinding时 将布局页面转换为数据绑定布局(Convert to data binding layout) 不出现提示解决办法
94 0
|
8月前
|
Android开发
Android 实现布局的缩小和再放大动画(使用scale动画效果进行实现)
Android 实现布局的缩小和再放大动画(使用scale动画效果进行实现)
587 0
|
8月前
|
XML Android开发 数据格式
Android Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView 错误
Android Binary XML file line #50: Error inflating class androidx.cardview.widget.CardView 错误
52 0