LinearLayout(线性布局)的基本使用

LinearLayout(线性布局) 直接为LinearLayout设置分割线这里就需要你自己准备一张线的图片了1)android:divider设置作为分割线的图片2)android:showDividers设置分割线的位置,none(无),beginning(开始),end(结束),middle(每两个组件间)3)dividerPadding设置分割线的Padding。代码里面明明有three的啊,还设置了3的,而1和2的比例也不对耶,1:2:3却变成了2:1:0,怎么会这样呢?android:text="保存"/> 阅读详情

LinearLayout又称作线性布局,是一种非常常用的布局。正如它的名字所描述的一样,这个布局会将它所包含的控件在线性方向上依次排列。既然是线性排列,肯定就不仅只有一个方向,这里一般只有两个方向:水平方向和垂直方向。

下面我们将LinearLayout(线性布局)常用到的属性简单归纳一下:

属性1:android:orientation    指定线性布局的方向(水平或者垂直)

属性2:android:width             线性布局的容器宽度

属性3:android:height           线性布局的容器高度

属性4:android:background  线性布局的背景

属性5:android:gravity          线性布局中,子容器相对于父容器所在的位置

 

1属性值:

android:orientation="horizontal"           指定线性布局方向:水平

android:orientation="vertical"               指定线性布局方向:垂直

2.属性值:

android:width="xxxdp"                          指定线性布局的容器宽度为:xxxdp

android:width="wrap_content"             指定线性布局的容器宽度为:根据容器内容宽度大小来填充屏幕宽度

android:width="match_parent"             指定线性布局的容器宽度为:撑满整个屏幕宽度

3.属性值:

android:height="xxxdp"                          指定线性布局的容器高度为:xxxdp

android:height="wrap_content"             指定线性布局的容器高度为:根据容器内容高度大小来填充屏幕高度

android:height="match_parent"             指定线性布局的容器高度为:撑满整个屏幕高度

4.属性值:

android:background="#000"                  指定线性布局的背景为:黑色(rgb颜色)

android:background="@android:color/black"   指定线性布局的背景为:黑色(引用android系统自带的原始黑色)

andrid:background="@color/colorPrimary"   指定线性布局的背景为:(根据res/color.xml 中的colorPrimary所定义的颜色设置)

5属性值:

android:gravity="center"      指定线性布局中,子容器相对于父容器所在的位置为:正中心

android:gravity="cente_verticalr"      指定线性布局中,子容器相对于父容器所在的位置为:垂直方向的正中心

android:gravity="center_horizontal"      指定线性布局中,子容器相对于父容器所在的位置为:水平方向的正中心

android:gravity="left"      指定线性布局中,子容器相对于父容器所在的位置为:最左边(默认)

android:gravity="right"      指定线性布局中,子容器相对于父容器所在的位置为:最右边

android:gravity="top"      指定线性布局中,子容器相对于父容器所在的位置为:最上方(默认)

android:gravity="bottom"      指定线性布局中,子容器相对于父容器所在的位置为:最下方

 

下面我们通过代码,分别看下水平和垂直方向上的效果图:

垂直方向:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3"
        android:textAllCaps="false" />

</LinearLayout>

垂直方向效果图:

 

水平方向:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3"
        android:textAllCaps="false" />

</LinearLayout>

水平方向效果图:

线性方向:水平,可以通过控件的android:layout_gravity属性的值来改变控件在垂直方向上的位置

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:text="btn1"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:text="btn2"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:text="btn3"
        android:textAllCaps="false" />

</LinearLayout>

效果图:

 

线性方向:水平,通过设置控件的android:layout_weight属性来设置控件与控件之间,在宽度上的权重比例大小,这里就将EditText控件与Button控件的宽度比例设置为1:1,前提是必须先将这两个控件的android:layout_width="0dp".

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/et1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type something" />

    <Button
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="send"
        android:textAllCaps="false" />

</LinearLayout>

效果图:

如果只是将EditText控件的

android:layout_width="0dp"   

android:layout_weight="1"

而Button控件的

android:layout_width="wrap_content“”

android:layout_weight属性不设置(注意这里没设置了)

 

代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/et1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Type something" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="send"
        android:textAllCaps="false" />

</LinearLayout>

效果图:

(使用layout_weight实现宽度自适配效果)

 

线性布局(Row/Column) 线性布局(LinearLayout)是开发中最常用的布局,通过线性容器Row和Column构建。线性布局是其他布局的基础,其子元素在线性方向上(水平方向和垂直方向)依次排列。线性布局的排列方向由所选容器组件决定,Column容器内子元素按照垂直方向排列,Row容器内子元素按照水平方向排列。根据不同的排列方向,开发者可选择使用Row或Column容器创建线性布局。图1Column容器内子元素排列示意图图2Row容器内子元素排列示意图。 阅读详情

相关推荐

(四)Android布局类型(线性布局LinearLayout)

Android布局类型:LinearLayout

以秘 1310

【Android】线性布局(LinearLayout)最全解析

【Android】线性布局(LinearLayout)最全解析一、LinearLayout概述二、LinearLayout常用属性2.1 orientation属性2.2 gravity属性2.3 layout_weight属性 一、LinearLayout概述 线性布局(LinearLayout)主要以水平或垂直方式来排列界面中的控件。并将控件排列到一条直线上。在线性布局中,如果水平排列,垂直方向上只能放一个控件,如果垂直排列,水平方向上也只能方一个控件。 使用线性布局,需要将布局节点改成LinearL

lucashu的博客 6万+

Qoder + ADB Supabase :5分钟GET超火AI手办生图APP

本文介绍如何利用Qoder、阿里云 ADB Supabase 和通义千问图像编辑模型,快速构建AI手办生图Flutter应用。无需传统后端,通过AI生成代码、BaaS服务与边缘函数调用,实现从原型到上线的敏捷开发,全流程轻量高效,助力Vibe Coding落地实践。

数据库技术 941

Android 基础知识4-2.6LinearLayout(线性布局)

线性布局(LinearLayout)主要以水平或垂直方式来排列界面中的控件。并将控件排列到一条直线上。在线性布局中,如果水平排列,垂直方向上只能放一个控件,如果垂直排列,水平方向上也只能方一个控件。使用线性布局,需要将布局节点改成LinearLayout,基本格式如下:......

yyxhzdm的博客 6183

经纬度转换为UTM坐标

UTM(通用横向墨卡托投影): 是一种以米为单位的坐标系统,用于地图和GPS导航。将地球划分为60个纵向的区域。每个区域宽6度,从赤道开始往南北两级方向划分。每个区域都有一个特定的字母。

weixin_45626706的博客 7298

Unity 多线程编程笔记 _Task详解

士大夫

weixin_43405845的博客 4390

【Android】——LinearLayout布局

线性布局(LinearLayout)???——》整个Android布局中的控件摆放方式是以线性的方式摆放的;   属性解析???——》 LinearLayout分类???——》 水平线性布局???——》android:orientation="horizontal" 垂直线性布局???——》android:orientation="vertical" ...

YHJ 4852

【多目标跟踪】ByteTrack复现(有yolov5例子)

1.环境 ubuntu16.04 cuda10.1 cudnn7 python3.6 Cython matplotlib>=3.2.2 numpy>=1.18.5 opencv-python>=4.1.2 Pillow PyYAML>=5.3 scipy>=1.4.1 tensorboard>=2.2 torch>=1.7.0 torchvision>=0.8.1 tqdm>=4.41.0 seaborn>=0.11.0 easydic

聿默的博客 1万+

Android线性布局Linearlayout

Android线性布局Linearlayout

Android应用开发工程师 1927

【雷达成像】基于matlab SAR雷达成像仿真【含Matlab源码 3750期】

SAR雷达成像仿真 完整的代码,方可运行;可提供运行操作视频!适合小白!

订阅付费专栏Matlab(奶茶价版),可赠送奶茶价版付费专栏指定代码1份; 1094

华为OD机试真题 新系统【小花获胜的奶茶】

华为OD机试真题 新系统【小花获胜的奶茶】,提供C语言、Java、C++、Python、Go、JavaScript源码实现,并提供完整思路讲解,以及在线OJ模拟做题。华为OD机试真题 【小花获胜的奶茶】多语言题解。

qq_45776114的博客 417

LinearLayout 线性布局

本文讲解了Android中LinearLayout线性布局的基本用法,通过一个完整的布局XML文件和Java代码示例,展示如何使用LinearLayout实现控件的垂直和水平排列,配合layout_weight属性实现空间的等比分配。适合Android初学者学习布局结构和UI界面搭建。内容涵盖LinearLayout嵌套、TextView控件使用、布局权重分配等实用技巧。运行效果清晰直观,适合在Android Studio中直接复制使用。

2402_84971234的博客 769

【WebRTC】【Unity】Unity Web RTC1-Unity中简单实现远程画面

平台:基本常用平台包括安卓,苹果(max和IOS),Windows64,Linux都行。10. 注意,如果从移动设备浏览器连接服务,信令服务器的SSL证书一定要先设好。想要在Unity中实现实时远程桌面,找到了Render Streaming这个手段,本篇介绍相应的使用方法和实例。8. 打开电脑浏览器,地址栏输入http://127.0.0.1,点击Receiver Sample。新建Unity项目,模板选Third Person,Project Name随意。启动App,控制台出现如下信息表示成功。

每日出拳老爷子的博客 2519

Android LinearLayout(线性布局)

本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局) 而今天我们要讲解的就是第一...

小屁孩大帅-保密的博客 1751

实战5:农作物产量预测 蔬菜产量预测 lstm 完整代码数据-可直接运行-计算机毕设

实战5:农作物产量预测 蔬菜产量预测 lstm 完整代码数据-可直接运行-计算机毕设

机器学习深度学习业余选手 1262

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

布局Layouts之LinearLayout线性布局

从Hello world!开始,我们一直都是在一种布局下学习的,当然,对于基础内容的学习,还是没有任何问题的!但——   在Android开发中UI设计也是十分重要的,当用户使用一个App时,最先感受到的不是这款软件的功能是否强大,而是界面设计是否赏心悦目,用户体验是否良好。也可以这样说,有一个好的界面设计去吸引用户的使用,才能让更多的用户体验到软件功能的强大。   那么,Android中几种

javaqun的博客 3704

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Android中LinearLayout线性布局使用详解

A:可以在按钮前添加一个空白View:

qq_39460057的博客 1561

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

android linearlayout布局,初识安卓之LinearLayout线性布局

0.前言在安卓的多种布局方式中,LinearLayout(线性布局)和RelativeLayout(相对布局)算是最为常用的两种方式了,这里只讲解线性布局,随后将会更新相对布局的相关知识。1.常用属性下面列出几种之后可能会用到LinearLayout中的属性,遗忘时查询即可。属性名意义orientation布局方式,有horizontal(水平布局)和vertical(垂直布局)两种方式id组件名...

weixin_29098391的博客 521

7 线性布局——LinearLayout

在上一节中,我们讲到了所有的 Layout 都是从 ViewGroup 继承而来,它可以包含若干 View 并按照指定的规则将这个 View 摆放到屏幕上。那么接下来的章节我们就来学习一下 Android 的 UI 布局,Android 原生有六大布局,分别是: LinearLayout(线性布局)、RelativeLayout(相对布局)、TableLayout(表格布局)、FrameLayout(帧布局)、AbsoluteLayout(绝对布局)、GridLayout(网格布局),我们从最简单实用的一个

MC 的技术空间 2752

Android-线性布局LinearLayout

是因为我们的父控件android:orientation="vertical"设置了vertical,所以,在垂直方向都需要按照顺序来,所以没有改变,我们将android:orientation="horizontal"修改为水平,这样就可以看到,android:layout_gravity="center_vertical"属性生效了,相反的,其他属性就无效了,我们需要在特定的情况使用这些属性。当给一个控件设置weight的时候,这个控件的width就是无效的,所以给width设置为0。

2301_81364903的博客 1482

(三)安卓开发中的线性布局(LinearLayout)详解

线性布局(LinearLayout)是 Android 开发中简单而强大的工具,通过gravity和等属性,可以灵活地实现各种界面需求。无论是简单的按钮排列,还是复杂的嵌套布局,它都能胜任。通过合理使用代码示例中的技巧,并结合具体场景,可以快速构建高效、直观的界面。同时,注意避免过度嵌套和性能问题,能让线性布局发挥最大价值。

cuijiying的博客 1540

android studio线性布局 LinearLayout

Android六大基本布局分别是:线性布局LinearLayout、表格布局TableLayout、相对布局RelativeLayout、层布局FrameLayout、绝对布局AbsoluteLayout、网格布局GridLayout。 在android开发中,用的相对频繁的是线性布局和相对布局,在大多数的情况下使用这两种布局都能完成。 一、线性布局LinearLayout最常用的属性有: a...

weixin_42265250的博客 3万+

线性布局之LinearLayout入门

线性布局之LinearLayout入门 前言 常用属性 举例子 注意点 一、前言 Android的界面组件比较多,为了更好地管理Android应用的用户界面里的各组件,Android提供了布局管理器。通过使用布局管理器,Android应用的图形用户界面具有良好的平台无关性。在这里简单介绍一下线性布局。 线性布局由LinearLayout类来代表,它会将容器里的元素(可...

fjnu_se的博客 463
上一篇: ProgressDialog(带进度条的对话框)的基本使用
下一篇: RelativeLayout(相对布局)的基本使用
Android_牧
博客等级 码龄9年 88粉丝 45原创
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值