Android LinearLayout横排和竖排布局
如果在layout中要不重叠地显示多个子LinearLayout,则必须在orientation中明确是vertical 还是horizontal。
下面这个例子,在LinearLayout中,两个子Layout是竖直排布的,代码和显示结果分别如下,
<?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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context="com.spacesoftwares.myapplication2.MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="150dp"><TextViewandroid:layout_width="300dp"android:layout_height="47dp"android:fontFamily="serif"android:text="I just want to write something here, don't do any thing first 支持中国汉字 显示 "android:textColor="#ff00ff" /><TextViewandroid:layout_width="105dp"android:layout_height="30dp"android:text="Hello my World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="200dp"><TextViewandroid:layout_width="300dp"android:layout_height="47dp"android:fontFamily="serif"android:text="I just want to write something here, don't do any thing first 支持中国汉字 显示 "android:textColor="#ff00ff" /></LinearLayout></LinearLayout>
显示结果:
如果要横排显示,代码就有所不同,主要有两个地方,一个是主LinearLayout的orientation要写成horizontal, 子LinearLayout的宽度就不能再是match_parent了,此时可以选择适当的比例。我这里通过调节权重layout_weight = “1”,让两个子LinearLayout等宽,当然也你可以加入第3个或第4个再尝试一下,具体实例可参考【1】,很简单
源码和效果如下,
<?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:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context="com.spacesoftwares.myapplication2.MainActivity"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="150dp"android:layout_weight="1"><TextViewandroid:layout_width="300dp"android:layout_height="47dp"android:fontFamily="serif"android:text="<span style="" font-size:9.0pt;"="">支持中国汉字 显示 "android:textColor="#ff00ff"android:background="#555555"/></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="150dp"android:layout_weight="1"><TextViewandroid:layout_width="300dp"android:layout_height="47dp"android:fontFamily="serif"android:text="<span style="" font-size:9.0pt;"="">支持中国汉字 显示 "android:textColor="#ff00ff"android:background="#000055"/></LinearLayout> </LinearLayout>
显示结果:
另外一种不使用weight,主LinearLayout也不使用orientation的方式,如下
<?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:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.spacesoftwares.myapplication2.MainActivity"> <LinearLayout android:layout_width="150dp" android:layout_height="100dp" > <TextView android:layout_width="150dp" android:layout_height="100dp" android:fontFamily="serif" android:text="支持中国汉字 显示 " android:textColor="#ff00ff" android:background="#555555" /> </LinearLayout> <LinearLayout android:layout_width="250dp" android:layout_height="100dp" > <TextView android:layout_width="250dp" android:layout_height="100dp" android:fontFamily="serif" android:text="支持中国汉字 显示 " android:textColor="#ff00ff" android:background="#000055" /> </LinearLayout> </LinearLayout>
显示效果如下
参考
【1】 https://blog.csdn.net/zhulichen/article/details/54313576
【2】 开发环境adroid studio 3.0, JRE1.8.0; 屏幕截图采用软件SpaceCapture v1.07a.
Android LinearLayout横排和竖排布局相关推荐
- Android学习笔记(11):线性布局LinearLayout
线性布局LinearLayout是指在横向或是竖向一个接一个地排列.当排列的组件超出屏幕后,超出的组件将不会再显示出来. LinearLayout支持的XML属性和相应方法如表所看到的: Attrib ...
- 关于android LinearLayout的比例布局(转载)
关于android LinearLayout的比例布局,主要有以下三个属性需要设置: 1,android:layout_width,android:layout_height,android:layo ...
- Android中常见五种布局管理器——RelativeLayout、LinearLayout、FrameLayout、TableLayout、GridLayout
目录 布局管理器 RelativeLayout 常见属性 Relative的实践操作(实现软件更新界面) LinearLayout 常见属性 LinearLayout的实践操作(模范登录以及微信底部) ...
- android 垂直水平居中对齐,Android开发借助LinearLayout实现垂直水平居中布局
释放双眼,带上耳机,听听看~! 在Android应用程序的开发中,有时需要限制水平和垂直屏幕的切换,今天这篇文章是技术狗小编为大家整理的Android应用借助LinearLayout实现垂直水平居中布 ...
- Android LinearLayout 各布局属性总结
LinearLayout是开发中使用率很高的控件,大部分开发人员感觉应该是很熟悉了,不过LinearLayout还有如下几个需要注意的地方,深刻了解以下几点可以让我们更加全面地认识LinearLayo ...
- Android LinearLayout布局
1. LinearLayout类 线性布局(LinearLayout)将子视图以线性方式显示.主要属性 orientation,horizontal代表水平方向,vertical代表垂直方向 weig ...
- android linearlayout最大高度,Android中最简单最常用的LinearLayout线性布局
原标题:Android中最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认为Relativ ...
- android设置水平布局,Android开发借助LinearLayout实现垂直水平居中布局
释放双眼,带上耳机,听听看~! 在Android应用程序的开发中,有时需要限制水平和垂直屏幕的切换,今天这篇文章是技术狗小编为大家整理的Android应用借助LinearLayout实现垂直水平居中布 ...
- Android 改变窗口标题栏的布局
Android改变窗口标题栏的布局 第一种方式 --在XML文件里面引入配置文件作为标题. 第二种方式 --动态的代码加入进去. 第三种方式(网上的): 一. 重点 一般应用的Title都是建立应 ...
最新文章
- python链表的创建_《大话数据结构》配套源码:链表(Python版)
- 最近miRNA研究上取得的新进展论文汇总
- #pragma warning(pop) 和 #pragma warning(push)的作用
- 卷积神经网络minst的verilog实现
- [Linux]信号量
- jquery scrollTop及其应用例子
- NetBeans 时事通讯(刊号 # 67 - Aug 04, 2009)
- Ora-01008错误:oracle 并非所有变量都已绑定的原因
- 2012年***名单。
- You can be happy no matter what.
- 支付宝APP退款功能开发
- MATLAB读取数据文件
- 【渝粤题库】广东开放大学 风险投资 形成性考核
- 一家快倒闭的茶馆如何通过一套商业模式改变现状,月盈利百万的方案分享!
- 如何深入编辑优化SPSS的统计图表——属性优化
- i5 13600K和i5 12600k差距
- 坯子库曲面推拉教程_坯子助手下载_坯子助手最新版下载-下载之家
- 摄像头的车牌识别(用hyperlpr的车牌识别,python版本)
- 顺丰速运——深圳大数据部一面(18分钟)
- 挖Linux中的古老缩略语
热门文章
- 【PAT】PAT官网更新
- TFRecord文件查看包含的所有Features
- html截取字符串的第4位,三种常用JS截取字符串方法 JS截取几位后字符串方法
- stata里php代码,stata命令求解惑
- 上海linux运维培训,在上海想学习Linux运维去哪里
- 大数乘法(C语言实现)
- java课程之团队开发冲刺阶段1.2
- 实战:部署一套完整的企业级高可用K8s集群(成功测试)-2021.10.20
- Transformer(4)
- Best Time to Buy and Sell Stock系列分析