小伙伴们,在上文中我们介绍了Android常见布局中的LinearLayout,本文我们继续盘点介绍Android开发中另一个常见的布局,相对布局RelativeLayout。
(2)参数 new RelativeLayout.LayoutParams()需要填写宽度和高度两个参数
大家好,又见面了,我是你们的朋友全栈君。Android Layout 之 RelativeLayout
RelativeLayout布局是Android界面布局中应用最广也最强大的一种布局,其不仅十分灵活,可以解决开发中各种界面布局需求,同时也很方便了解决了多屏幕尺寸的适配问题。在iOS开发中,Autolayout技术总是被赞不绝口,RelativeLayout布局就是Andriod系统中的Autolayout,其又被称为相对布局。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160830.html原文链接:https://javaforall.cn
其实LayoutParams就是代表一个布局属性,每一个ViewGroup对应一种LayoutParams。LinearLayout对应LinearLayout.LayoutParams,RelativeLayout对应RelativeLayout.LayoutParams。我们在XML中写的大多数属性,在代码中通过LayoutParams同样可以操作界面布局。下面以RelativeLayout.LayoutParams为例: (1)RelativeLayout.LayoutParams.setMargins(left, top, right, bottom) 通过此方法可以动态设置margin值; (2)addRule(int verb) 该方法表示所设置节点的属性不能与其他兄弟节点相关或者属性值为布尔值。 比如 addRule(RelativeLayout.CENTER_VERTICAL)就表示在RelativeLayout中的相应节点是垂直居中的。 (3)addRule(int verb,int anchor)该方法表示所设置节点的属性必须关联其他兄弟节点或者属性值为布尔值。 比如addRule(RelativeLayout.ALIGN_LEFT,R.id.date)就表示RelativeLayout中的相应节点放置在一个id值为date的兄弟节点的左边。
1、无xml : 一个父类布局包含一个子父类布局,子父类布局中包含ImageView
RelativeLayout 和 LinearLayout 类似,都是 ViewGroup,能“容纳”多个子view。
FrameLayout的父控件是一个LinearLayout控件,问题出在,LinearLayout为子控件分配空间的时候,获取FrameLayout的LayoutParams的必须为LinearLayout.LayoutParams,而非FrameLayout.LayoutParams。
主要思路是通过父布局的onTouch(),方法,获取滑动到的位置和点击下的位置,再去设置子view的位置。我的代码中考虑了在边缘情况。需要注意的是,使用RelativeLayout,以imageView为例。从测试结果来看,bottomMargin 和rightMargin 性能非常差,最好还是用leftMargin与topMargin定位。
在使用RelativeLayout布局的时候,通常在载入布局之前在相关的XML文件中进行静态设置即可。但是,在有些情况下,我们需要动态的设置布局的属性,在不同条件下设置不同的布局排列方式。这个时候就需要使用到RelativeLayout.LayoutParams.addRule()方法,该方法有两种重载方法:
本节继续带来的是Android系统服务中的LayoutInflater(布局服务),说到布局,大家第一时间 可能想起的是写完一个布局的xml,然后调用Activity的setContentView()加载布局,然后把他显示 到屏幕上是吧~其实这个底层走的还是这个LayoutInflater,用的Android内置的Pull解析器来解析 布局。一般在Android动态加载布局或者添加控件用得较多,本节我们就来学习下他在实际开发中 的一些用法~
同时MarginLayoutParams extends LayoutParams 也作为ViewGroup类里面的静态内部类
在日常的Android开发当中,我们少不了需要动态改变控件在RelativeLayout界面的位置。那么我们就需要使用到RelativeLayout.LayoutParams。
layout_constraintHorizontal_bias layout_constraintVertical_bias
1.首先,我们已经知道通过addView这个方法可以动态的添加自己新建的一个view,例如activityLayout.addView(new Button());这样就可以添加一个新的button,而且在添加时可以规定新的view的位置和大小,通过RelativeLayout.LayoutParams控制,例如 RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( (int) (100 * metrics.density
获取房子,菜单图标ImageView对象,获取三个圆环RelativeLayout对象
本文实例为大家分享了Android实现弹窗广告的具体代码,供大家参考,具体内容如下
需求:视频通话界面,两个surfaceView一个显示本端的视图,另一个显示对端的视图,由于显示比例的问题总会存在一个覆盖另一个的问题,为保证用户体验,规定小的覆盖大的视图上面,且点击小的视图可切花为大图视图居中,达到两个视图切花的功能。简单写一个demo完成功能的测试需求,为了较少文章的篇幅,视图的内容用回执矩形代替(实际开发中显示的是本地照相采集的数据和对端经过opgl处理的数据)
本文实例为大家分享了Android自定义view实现TopBar的具体代码,供大家参考,具体内容如下
做项目需要播放一个引导视频,本以为很简单,结果动手时发现总有瑕疵,幸好有度娘,现把收获总结如下:
主要的类:继承了RelativeLayout,再在RelativeLayout里面添加ImageView,通过Touch事件来改变ImageView的缩放,缩放时计算scale,使其在手指移动到屏幕底部时,图片底部也刚好到达屏幕底部,手指松开时,图片逐步回弹。
实现这样的布局一定要用到RelativeLayout 相对布局,我们这样指定我的布局。
2.完全进去后,左上角有个放大镜的窗口,点击搜索框,输入抖音ID(191433445)
本文实例为大家分享了Android原生视频播放VideoView的具体代码,供大家参考,具体内容如下
Android的UI设计可以说是决定一个app质量的关键因素,因为人们在使用app的时候,最先映入眼帘的就是app的界面了,一个美观、充实的界面能够给用户带来非常好的体验,会在用户心中留下好的印象。 对于UI设计,Android原生的控件加上一些开源库的使用,已经能够满足大部分的UI需求,但是,某些比较新颖、花哨的控件效果,我们只能通过自定义View来实现,那么,从该篇博客开始,我将记录关于Android自定义View的学习内容,并将其整理呈现给大家。 我们来实现一个优酷菜单案例,在案例中会涉及到很多方面的知识。 案例效果如下:
在前面三期中我们对LinearLayout进行了详细的解析,LinearLayout也是我们用的比较多的一个布局。但在实际开发中使用LinearLayout远远不够,我们本期一起来学习RelativeLayout。 一、认识RelativeLayout RelativeLayout,又叫相对布局,使用<RelativeLayout>标签。相对布局通常有两种形式,一种是相对于容器而言的,一种是相对于控件而言的。 下表显示了RelativeLayout支持的常用XML属性及相关方法的说明
2013年8月14日Android记录 很多应用都会有用户设置,用户的一些偏好可以由用户来决定那是应用人性化的体现,在实际开发中很多情况都作成可配置的了,本篇博客要介绍的是一个比较炫的状态按钮切换,我
listview的上滑下滑监听,来隐藏和显示顶部选项栏的特效,京东 同程等APP的资源列表都有此特效.
写过 UI的同学一定知道 layout_gravity和 gravity这两个属性,
先明确几个概念的区别: padding margin:都是边距的含义,关键问题得明白是什么相对什么的边距 padding:是控件的内容相对控件的边缘的边距.
多层布局的嵌套会导致页面加载慢,影响用户的体验,今天我们就来学学如何使用 include,merge及viewStub。
实现不改变activity,只切换View 抽取View界面的基类 利用面向对象多态的思路,实现通用 TitleManager.java 管理标题 package com.tsh.lottery.vi
在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById()。不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化;而findViewById()是找xml布局文件下的具体widget控件(如Button、TextView等)。
今天碰到这个error:E/AndroidRuntime( 4657): Uncaught handler: thread main exiting due to uncaught e xception E/AndroidRuntime( 4657): java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout 有点郁闷,我用的是skd1.5,在1.5的机器上(HTC G3)已经测试过了,没有问题的,但放在华为c8500(2.1update)上就报上面的错了,怎么回事呢? 根据提示判断应该是布局的原因,于是找到RelativeLayout的布局,找出最可疑的那个,注释后,不报错了。好就是他的原因,挨个看里面的元素,看属性,没错啊,后来发现, <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true"> <TextView android:id="@+id/titleName" android:text="首页" android:textColor="@color/white" android:layout_toLeftOf="@+id/homeBtn" android:layout_marginRight="2px" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ImageButton android:id="@+id/homeBtn" android:layout_toRightOf="@+id/titleName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/main" android:background="@null" android:layout_marginRight="10px"> </ImageButton> </RelativeLayout> 后来改成: <RelativeLayout android:layout_width="wrap_content" android:layout_marginRight="10px" android:layout_height="wrap_content" android:layout_alignParentRight="true"> <TextView android:id="@+id/titleName" android:text="首页" android:textColor="@color/white" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> <ImageButton android:id="@+id/homeBtn" android:layout_marginLeft="2px" android:layout_marginTop="2px" android:layout_toRightOf="@+id/titleName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/main" android:background="@null" > </ImageButton> </RelativeLayout> 能看到区别吗?对就是在titleName中去掉了相对homeBtn的位置信息。再看看报错提示,人家说我在RelativeLayout中存在循环的相关,就是说的这个了。 不过1.5版本的不报错,这就是后续版本的改进吗?
在网易严选的看东西的时候在商品详情页里看到他的底部弹出菜单,本能反应是想用DottomSheetDialog或者PopupWindow来实现,可是发现实现不了他那种效果,于是就自己模仿一个像严选这样的底部弹出菜单。
如今上网看电影电视越发流行了,追剧的时候经常看到视频上方数行评论向左边飘去,犹如子弹那样飞快掠过,这些评论文字因此得名“弹幕”。弹幕评论由正在观看的网友们即兴发表,故而连绵不绝从画面右端不断涌现,直到漂至画面左端才隐没消失。 虽然弹幕效果可使用平移动画实现,但平移动画比较单调,只能控制位移,不能控制速率、文字大小、文字颜色等要素。若想同时操纵视图的多种属性要素,需要采用属性动画加以实现。 然而视图的位移大小由间距属性margin控制,该属性又分为上下左右四个方向,更要命的是,这几个margin并非视图View类的属性,而是布局参数LayoutParams的属性,意味着无法通过margin***直接构造属性动画对象。为了动态调整margin这种非常规属性,就要引入估值器实时计算当前的属性值,再据此设置自定义控件的状态参数。 以间距属性为例,它的动画步骤说明如下: 1、定义一个间距估值器,它实现了接口TypeEvaluator的evaluate方法,并在该方法中返回指定时间点的间距数值; 2、调用ValueAnimator类的ofObject方法,根据间距估值器、开始位置和结束位置构建属性动画对象; 3、调用属性动画对象的addUpdateListener方法设置刷新监听器,在监听器内部获取当前的间距数值,并调整视图此时的布局参数; 具体到编码实现上,需要自定义弹幕视图,其内部在垂直方向排列,每行放置一个相对布局。发表弹幕评论时,先随机挑选某行相对布局,在该布局右侧添加文本视图,再通过前述的间距动画向左渐次滑动。弹幕视图的定义代码示例如下:
以下代码改变TextView的margin和CENTER_HORIZONTAL属性。
转载请声明出处,谢谢!https://cloud.tencent.com/developer/user/1148436/activities 先上效果图,给大家个直观效果,后上实现代码: ->
/**********************2016年4月23日 更新*******************************/
针对 页面布局的性能、层级、测量绘制时间 进行优化,从而提高 Android应用中的页面显示速度
当多个界面都有很多相似部分时,可以考虑创建一个功能较全的模板。而在需要时,可以通过引用模板来实现自己想要实现的功能。比如适配器 Adapter,当很多的适配器都差不多时,就可以通过打造一个通用的适配器来实现。本例中主要是如何创建自定义的 ActionBar。
布局视图有五类,分别是线性布局LinearLayout、相对布局RelativeLayout、框架布局FrameLayout、绝对布局AbsoluteLayout、表格布局TableLayout。其中最常用的是LinearLayout,它适用于包括简单布局在内的多数情况;其次常用的是RelativeLayout,它适用于一些复杂布局,主要是对相对位置要求较多的情况;再次就是FrameLayout,它一般用于需要叠加展示的场合,比如说给整个页面设置一个背景布局等等。AbsoluteLayout和TableLayout实际中很少用,基本不用关心。 另外还有纵向滚动视图ScrollView,以及横向滚动视图HorizontalScrollView,其作用顾名思义便是让它们的子视图可以在某个方向上滚动罢了。
View ==> ViewGroup ==> RelativeLayout,主体部分使用RelativeLayout作为占位
1: 当没有没完成进度的时候,显示效果一 2:当进度完成了一部分,显示图二 3:当进度全部完成之后,显示效果三 4:当进度1到进度2需要动画,进度2到进度3需要动画; 同样进度3到进度2或者进度1也需要动画。
效果图 不多废话, 先上图, 有兴趣再看下去: 效果图 前言 用代码增删布局还是很常用的. 布局文件 先来看看布局文件, 不是很复杂, 但是涉及到之后java部分的代码, 所以必须都贴出
效果图 不多废话, 先上图, 有兴趣再看下去: 效果图 前言 这篇是之前的一篇旧文改的, 也是想将这篇放入自己的界面无小事专题, 所以当成新篇章来写, 绝对不是为了什么日更之类的事情哦(手动滑
大家好,今天我为大家介绍一下如何在Android系统下实现实时直播互动中的多视频展示功能。 我们现在都清楚实时直播互动中多路视频的展示是目前比较流行的趋势,因为大家都想看到更多视频画面的需求,是非常符合人类的习惯的。
LayoutParams继承于Android.View.ViewGroup.LayoutParams. LayoutParams相当于一个Layout的信息包,它封装了Layout的位置、高、宽等信息。假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉Layout用户期望的布局方式,也就是将一个认可的layoutParams传递进去。 可以这样去形容LayoutParams,在象棋的棋盘上,每个棋子都占据一个位置,也就是每个棋子都有一个位置的信息,如这个棋子在4行4列,这里的“4行4列”就是棋子的LayoutParams。
本文实现viewpager图片轮播的功能、左右滑动的时候能够流畅的切换图片、并且没有边界限制
领取专属 10元无门槛券
手把手带您无忧上云