android layout布局 有哪些,android 常用布局 介绍与使用[亲测有效] -金沙1005

android layout布局 有哪些,android 常用布局 介绍与使用[亲测有效]读前思考学习一门技术或者看一篇文章最好的方式就是带着问题去学习,这样才能在过程中有茅塞顿开、灯火阑珊的感觉,记忆也会更深刻。有哪些常用的布局?每一种布局有何特点与不同?布局上如何优化?1.约束布局constraintlayoutconstraintlayout是androidstudio2.2中主要的新增功能之一,constraintlayout使用约束的方式来指定各个控件的位置和…

读前思考

学习一门技术或者看一篇文章最好的方式就是带着问题去学习,这样才能在过程中有茅塞顿开、灯火阑珊的感觉,记忆也会更深刻。

有哪些常用的布局?

每一种布局有何特点与不同?

布局上如何优化?

1. 约束布局 constraintlayout

constraintlayout 是 android studio 2.2 中主要的新增功能之一,constraintlayout 使用约束的方式来指定各个控件的位置和关系的,它有点类似于 relativelayout,但远比 relativelayout 要更强大,它可以有效地解决布局嵌套过多的问题。

本文主要讲解通过 xml 的编写实现 constraintlayout,如果想要了解拖拽方式,可参考 android新特性介绍,constraintlayout完全解析

1. 首先用 constraintlayout 实现置顶,高自适应,宽 match_parent

android:id=”@ id/tv_name”

android:layout_width=”0dp”

android:layout_height=”wrap_content”

android:gravity=”center”

android:text=”姓名”

android:textsize=”20sp”

app:layout_constraintleft_toleftof=”parent”

app:layout_constraintright_torightof=”parent”

app:layout_constrainttop_totopof=”parent”

/>

复制代码

实现效果图

e5343397b30fafd6b61256ac9dd34943.png

2. 实现一个控件在另一个控件下方

android:id=”@ id/tv_mobile”

android:layout_width=”wrap_content”

android:layout_height=”20dp”

android:text=”手机号”

android:gravity=”center”

app:layout_constraintleft_toleftof=”parent”

app:layout_constraintright_torightof=”parent”

app:layout_constrainttop_tobottomof=”@id/tv_name”

/>

复制代码

实现效果图

191cc33eee2af8510bd0cb501b8e9d66.png

3. 实现控件上下左右居中显示

android:id=”@ id/tv_age”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:text=”年龄”

android:gravity=”center”

android:textsize=”30sp”

app:layout_constrainthorizontal_weight=”3″

app:layout_constraintleft_toleftof=”parent”

app:layout_constraintright_torightof=”parent”

app:layout_constrainttop_totopof=”parent”

app:layout_constraintbottom_tobottomof=”parent”

/>

复制代码

实现效果

61a8a0dab8685381689423dea44edcbd.png

通过布局权重,实现百分比布局

android:id=”@ id/tab0″

android:layout_width=”0dp”

android:layout_height=”50dp”

android:background=”@color/colorprimary”

android:gravity=”center”

android:text=”tab1″

android:textcolor=”@color/coloraccent”

android:textsize=”20sp”

app:layout_constrainthorizontal_weight=”2″

app:layout_constraintbottom_tobottomof=”parent”

app:layout_constraintleft_toleftof=”parent”

app:layout_constraintright_toleftof=”@ id/tab1″ />

android:id=”@ id/tab1″

android:layout_width=”0dp”

android:layout_height=”0dp”

android:gravity=”center”

android:text=”tab2″

android:textsize=”20sp”

app:layout_constrainthorizontal_weight=”1″

app:layout_constraintbottom_tobottomof=”parent”

app:layout_constraintleft_torightof=”@ id/tab0″

app:layout_constraintright_toleftof=”@ id/tab2″

app:layout_constrainttop_totopof=”@ id/tab0″ />

android:id=”@ id/tab2″

android:layout_width=”0dp”

android:layout_height=”0dp”

android:background=”@color/coloraccent”

android:gravity=”center”

android:text=”tab3″

app:layout_constrainthorizontal_weight=”1″

android:textcolor=”@color/colorprimary”

android:textsize=”20sp”

app:layout_constraintbottom_tobottomof=”parent”

app:layout_constraintleft_torightof=”@ id/tab1″

app:layout_constraintright_toleftof=”@id/tab3″

app:layout_constrainttop_totopof=”@ id/tab0″ />

android:id=”@ id/tab3″

android:layout_width=”0dp”

android:layout_height=”0dp”

android:gravity=”center”

android:text=”tab4″

android:textsize=”20sp”

app:layout_constrainthorizontal_weight=”3″

app:layout_constraintbottom_tobottomof=”parent”

app:layout_constraintleft_torightof=”@ id/tab2″

app:layout_constraintright_toleftof=”@ id/tab4″

app:layout_constrainttop_totopof=”@ id/tab0″ />

app:layout_constrainthorizontal_weight=”1″

android:id=”@ id/tab4″

android:layout_width=”0dp”

android:layout_height=”0dp”

android:background=”@color/coloraccent”

android:gravity=”center”

android:text=”tab5″

android:textcolor=”@color/colorprimary”

android:textsize=”20sp”

app:layout_constraintbottom_tobottomof=”parent”

app:layout_constraintleft_torightof=”@ id/tab3″

app:layout_constraintright_torightof=”parent”

app:layout_constrainttop_totopof=”@ id/tab0″ />

复制代码

实现效果图

66a66aa79b43266a206e0681aaaa5563.png

2. 线性布局 linearlayout

线性布局是按照水平或垂直的顺序将子元素(可以是控件或布局)依次按照顺序排列,每一个元素都位于前面一个元素之后。线性布局分为两种:水平方向和垂直方向的布局。分别通过属性 android:orientation=”vertical” 和 android:orientation=”horizontal” 来设置。 android:layout_weight 表示子元素占据的空间大小的比例。

接下来我们来实现如下效果

a932a3d73560f9c93e121ded9f42124e.png

具体代码实现

xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:background=”#ffffff”

android:orientation=”vertical”>

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:orientation=”horizontal” >

android:id=”@ id/msg”

android:inputtype=”number”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:text=””>

// 第二行为 mc m m- mr 四个button构成一个水平布局

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:orientation=”horizontal” >

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:text=”mc” android:layout_weight=”1″>

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:text=”m ” android:layout_weight=”1″>

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:text=”m-” android:layout_weight=”1″>

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:text=”mr” android:layout_weight=”1″>

复制代码

3. 相对布局 relativelayout

relativelayout 继承于 android.widget.viewgroup,其按照子元素之间的位置关系完成布局的,作为 android 系统五大布局中最灵活也是最常用的一种布局方式,非常适合于一些比较复杂的界面设计。

接下来我们来实现如下效果

bdc96a49748c6d4816a5abbeee0cf506.png

具体代码如下

xmlns:android=”http://schemas.android.com/apk/res/android”

android:layout_width=”match_parent”

android:layout_height=”match_parent”>

android:id=”@ id/btn1″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_centerhorizontal=”true”

android:layout_centerinparent=”true”

android:text=”button1″

/>

android:id=”@ id/btn2″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_above=”@id/btn1″

android:layout_toleftof=”@id/btn1″

android:text=”button2″

/>

android:id=”@ id/btn3″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_above=”@id/btn1″

android:layout_torightof=”@id/btn1″

android:text=”button3″

/>

android:id=”@ id/btn4″

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_above=”@id/btn2″

android:layout_toleftof=”@id/btn3″

android:layout_torightof=”@id/btn2″

android:text=”button4″

/>

复制代码

由于我在项目中很少使用下面的布局,就不过多介绍了

4. 表格布局 tablelayout

表格布局,适用于多行多列的布局格式,每个 tablelayout 是由多个 tablerow 组成,一个 tablerow 就表示 tablelayout 中的每一行,这一行可以由多个子元素组成。实际上 tablelayout 和 tablerow 都是 linelayout 线性布局的子类。但是 tablerow 的参数 android:orientation 属性值固定为 horizontal,且 android:layout_width=match_parent,android:layout_height=wrap_content。所以 tablerow 实际是一个横向的线性布局,且所以子元素宽度和高度一致。

5. 框架布局 framelayout

framelayout 是最简单的布局了。所有放在布局里的控件,都按照层次堆叠在屏幕的左上角。后加进来的控件覆盖前面的控件。

在 framelayout 布局里,定义任何空间的位置相关的属性都毫无意义。控件自动的堆放在左上角,根本不听你的控制。但是控件本身是可以控制自己内部的布局的。

6. absolutelayou 绝对布局

绝对布局中将所有的子元素通过设置 android:layout_x 和 android:layout_y 属性,将子元素的坐标位置固定下来,即坐标 (android:layout_x, android:layout_y) ,layout_x 用来表示横坐标,layout_y 用来表示纵坐标。屏幕左上角为坐标 (0,0),横向往右为正方,纵向往下为正方。实际应用中,这种布局用的比较少,因为 android 终端一般机型比较多,各自的屏幕大小。分辨率等可能都不一样,如果用绝对布局,可能导致在有的终端上显示不全等。

布局优化

使用 include 标签加载重复布局

使用 merge 标签减少布局嵌套

使用 viewstub 动态控制布局显示

由于篇幅问题就不对上述做具体实例,自己可以尝试着实现。

文章已经读到末尾了,不知道最初的几个问题你都会了吗?如果不会的话?可以再针对不会的问题进行精读哦!答案都在文中,相信你肯定可以解决的!

js555888金沙老品牌的版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由思创斯整理,转载请注明出处:https://ispacesoft.com/140951.html

(0)

相关推荐

  • android 图片文字识别_怎么识别图片最近做了一款android应用需要输入大量的数据,为了提高体验我想了很多种输入数据的方式,最终采用了两种:二维码扫描和图片识别。前者顾名思义有个短板,就是需要生成二维码,下面就介绍下图片文字识别实现。本应用是基于是ocr引擎,故需要第三方的jar包tess-two.tesseract3.01-leptonica1.68-libjpeg6b.jar下载链接:点击打开链接另外tessdat…

  • android开发环境配置(以windows为例)[通俗易懂]android开发环境配置工具   如果你准备从事android开发,那么无论选择在eclipse下开发,还是选择在androidstudio下开发,都可以参照以下步骤进行android开发环境的配置。android开发环境配置过程1.准备笔记本或台式机  使用笔记本还是台式机,视个人需求而定,但我要强调的是在配置上不要手软,要舍得下手。一台流畅的电脑,会让

  • android 获取时间「建议收藏」在项目开发中,很多时候会用到android的时间,罗列一下获取的时间的方式,和大家共同学习进步一、获取系统时间1.通过calendar类来获取系统当前的时间calendarcalendar=calendar.getinstance();longunixtime=calendar.gettimeinmillis();//这是时间戳logger.i(tag,”cale

  • android 工具类toast,android toast工具类[亲测有效]释放双眼,带上耳机,听听看~!importandroid.content.context;importandroid.view.viewgroup;importandroid.widget.toast;/***toast统一管理类**/publicclasstoastutils{privateviewgroup.layoutparamslayoutparams;privateto…

  • android 使用retrofit2问题汇总retrofit2引用依赖问题compile’io.reactivex:rxjava:1.1.3’compile’io.reactivex:rxandroid:1.1.0’compile’com.squareup.retrofit2:retrofit:2.0.2’compile’com.squareup.retrofit2:converter-gson:

  • 一帧图像的android之旅 : 应用的首个绘制请求android 框架提供了各种用 2d 和 3d 图形渲染的 api 与制造商的图形驱动程序实现方法交互,在android平台上应用开发者可通过

  • android播放器推荐_ijkplayer httpsandroid播放器:mediaplayerexoplayerijkplayer比较

  • android怎么打开_android怎么用okhttp和okio文本将介绍okhttp和okio基本使用okhttphttp是现在app访问网络最流行的方式。通过它我们可以交换数据和媒体信息。而高效的使用http可以让你的加载数据更快并且节省带宽。okhttp就是一种http客户端连接,它有如下特性:http/2多路复用socket到同一个主机,共享链接。采用连接池技术,可以有效的减少http链接数量。无缝集成gzip压缩技术。支持re…

发表回复

您的电子邮箱地址不会被公开。

联系金沙1005

关注“java架构师必看”公众号

回复4,添加站长微信。

附言:ispacesoft.com网而来。

关注微信
网站地图