Android 六大布局之 LinearLayout( 线性布局)
目录
LinearLayout( 线性布局) 检索
weight( 权重)属性详解
最简单方式
wrap_content 方式
LinearLayout 设置分割线
图片分隔
LinearLayout( 线性布局) 检索
1、Android 中有六大布局: LinearLayout(线性布局),RelativeLayout(相对布局),TableLayout(表格布局) FrameLayout(帧布局),AbsoluteLayout(绝对布局),GridLayout(网格布局)
2、屏幕适配使用的比较多的就是 LinearLayout 的 weight(权重属性),本文详细地解析 LinearLayout,包括一些基本的属性,Weight 属性的使用,以及比例如何计算,还有使用 android:divider 绘制下划线!
本文使用的是 Android Studion 3.1.2
weight( 权重)属性详解
最简单方式
实现 图1.1 效果的代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="1"android:background="#ADFF2F" /><LinearLayoutandroid:layout_width="0dp"android:layout_height="fill_parent"android:layout_weight="2"android:background="#DA70D6" /></LinearLayout>
android:id="@+id/LinearLayout1" :为该资源控件设置一个资源 id,在 Java 代码中可以通过 findViewById(id) 找到该控件
android:layout_width="match_parent":布局的宽度设置为填满父容器
android:layout_height="match_parent":布局的高度设置为填满父容器
android:orientation="horizontal":布局中控件的排列方式设置为水平方向
android:layout_height="fill_parent:布局高度设置为充满父容器
tools:context=".MainActivity":声明当前布局提供给 activity 使用
android:layout_weight:设置权重,按比例划分水平方向,将涉及到的 View 的 android:layout_width 属性设置为 0dp,然后使用 android:layout_weight 属性设置比例即可,如上所示,第二个 LinearLayout 是第一个的两倍。
以此类推,竖直方向,只需设 android:layout_height 为 0dp,然后设置 layout_weight 属性即可,如果要达到 1:1 等分效果,则显然只需要分别把两个 LinearLayout 的 weight 改成1和1就可以了。
上图 1.2 效果代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#ADFF2F" /><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="0dp"android:layout_weight="1"android:background="#DA70D6" />
</LinearLayout>
wrap_content 方式
1、如果不用上述最简单的那种设置为 0dp 的方式,直接用 wrap_content 也是可以的——内容包裹
2、LinearLayout 的 orientation 属性决定向哪个方向等比例划分,horizontal(水平)或是 vertical(竖直)
实现上图 1.3 效果代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"tools:context=".MainActivity"><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="1"android:background="#98FB98"android:text="one" /><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="2"android:background="#FFFF00"android:text="two" /><TextViewandroid:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_weight="3"android:background="#FF00FF"android:text="three" />
</LinearLayout>
<LinearLayout :表示整个窗口采用线性布局
<TextView:位于线性布局中的文本控件
android:orientation:"horizontal":线性布局采用水平切分
<TextView android:layout_width="wrap_content":文本控件水平方向采用包裹内容
<TextView android:layout_height="fill_parent":文本控件垂直方向采用填满父容器
android:layout_weight="1"、"2"、"3":设置三个文本控件的权重,即第二个是第一个两倍,第三个是第一个的三倍
以此类推,其余各种比例设置以及垂直方向也是同理。
上图 1.4 效果代码如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="#98FB98"android:text="one" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:background="#FFFF00"android:text="two" /><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="2"android:background="#FF00FF"android:text="three" />
</LinearLayout>
LinearLayout 设置分割线
很多界面开发中都会设置一些下划线,或者分割线,从而使得界面更加整洁美观:
<view 分隔
对于这种线,做法 ① 直接在布局中添加一个view,作用仅仅是显示出一条线,代码也很简单:
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="#000000" />
实现上图 1.5 效果代码如下,这个是水平方向上的红色线,当然也可以改成其他颜色,或者使用图片:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1" /><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:background="#f00" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮2" /><Viewandroid:layout_width="match_parent"android:layout_height="1px"android:background="#f00" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮3" /></LinearLayout>
图片分隔
使用 LinearLayout 的一个 divider 属性,直接为 LinearLayout 设置分割线
1)自己先准备一张分割线的图片
2)android:divider 设置作为分割线的图片
3)android:showDividers 设置分割线的位置,none(无)、begining(开始)、end(结束)、middle(每两个组件之间)
4)android:dividerPadding 设置分割线的Padding
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/LinearLayout1"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:divider="@drawable/cut_off_rule"android:showDividers="middle"android:dividerPadding="5dp"tools:context=".MainActivity"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 1" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 2" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Button 3" />
</LinearLayout>
上述代码效果如下:
Android 六大布局之 LinearLayout( 线性布局)相关推荐
- 常见界面布局之LinearLayout线性布局
1.什么是LinearLayout线性布局 LinearLayout (线性布局)通常指定布局内的子控件水平或者轻直排列.在XML布局文件中定义线性布局的基本语法格式如下: <LinearLay ...
- Android 应用开发(36)---LinearLayout(线性布局)
LinearLayout(线性布局) 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout(相对布局),Table ...
- 【Android 】零基础到飞升 | LinearLayout(线性布局)
2.2.1 LinearLayout(线性布局) 本节引言 本节开始讲Android中的布局,Android中有六大布局,分别是: LinearLayout(线性布局),RelativeLayout( ...
- Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件...
UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个用于存放 ...
- 精通android布局,Android精通:View与ViewGroup,LinearLayout线性布局,RelativeLayout相对布局,ListView列表组件...
标题图 UI的描述 对于Android应用程序中,所有用户界面元素都是由View和ViewGroup对象构建的.View是绘制在屏幕上能与用户进行交互的一个对象.而对于ViewGroup来说,则是一个 ...
- Android零基础入门第25节:最简单最常用的LinearLayout线性布局
原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...
- android线性布局设置控件固定在底部,Android UI组件LinearLayout线性布局详解
LinearLayout 线性布局,该布局的继承关系: 1. 什么是线性布局 通俗的说感觉起来和线有关,参照线的特点,有么是横向的,要么是竖向的. LinearLayout是线性布局控件,它包含的子控 ...
- Android布局——LinearLayout 线性布局
简介 贴上官方文档:线性布局 | Android 开发者 | Android Developers Linear 意为 线形的,线状的,所以该布局具有线的特点,要么是横向的,要么是竖向的. 该 ...
- Android布局——Linearlayout线性布局
Android最简单最常用的LinearLayout线性布局 一.认识LinearLayout 线性布局是Android中较为常用的布局方式,使用LinearLayout标签.线性布局主要有两种形式, ...
- android linearlayout最大高度,Android中最简单最常用的LinearLayout线性布局
原标题:Android中最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认为Relativ ...
最新文章
- 图解自监督学习,人工智能蛋糕中最大的一块
- 区块链系列4-智能合约
- Spring Security OAuth笔记
- 闰年的判断方法_C语言100题 判断这一天是一年中的第几天 壁纸 火影
- PLSQL Developer连接oracle数据库安装及配置
- [NOTE] SQL与sqlmap
- oracle过滤某个字段重复记录,只获取一条记录
- activex java 控件_java 如何调用ActiveX控件??
- Python 获取md5值(hashlib)
- [NOIp 2009]Hankson的趣味题
- 牛腩学ASP.NET CORE做博客(视频)
- 2021-09-08推荐系统有如下三大类算法
- GhostScript 沙箱绕过(命令执行)漏洞(CVE-2018-19475)复现
- java 双引号 转义_java字符转义 字符串中的双引号
- 5-(4-硝基苯基)-10,15,20-三苯基卟啉NTPPH2/NTPPZn/NTPPMn/NTPPFe/NTPPNi/NTPPCu/NTPPCd/NTPPCo等金属配合物
- mysql数据库从入门到高级
- 地理信息系统(GIS)系列——Portal for ArcGIS
- leetcode第六题 Z字形变换
- 2022温州经开区沙城街道办事处招聘编外人员综合试题及答案
- CR总结--java函数CR
热门文章
- Create Material by BDC and BAPI
- 机器学习之K-means算法
- html给看板娘添加语音,用html代码给网页加个live2d看板娘吧
- 拓端tecdat|R语言绘制ggplot2双色XY-面积图组合交叉折线图可视化
- 拓端tecdat|R语言中回归模型预测的不同类型置信区间应用比较分析
- 拓端tecdat|r语言中对LASSO回归,Ridge岭回归和弹性网络Elastic Net模型实现|视频
- java 获取远程系统启动时间_从Java中的RuntimeMXBean获取系统启动时间
- 【Pycharm】连接远程服务器并进行代码上传、调试
- 利用opencv生成面膜
- Python机器学习库sklearn自动特征选择(训练集)