Android中如何使控件保持固定宽高比
目录
1、自定义view
2、adjustViewBounds
3、百分比布局
4、ConstraintLayout
我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但是组件本身大小却不定。尤其在android屏幕碎片化的情况下,很多时候我们需要让一个组件宽度与屏幕宽度一致,这样就无法确定宽度。那么如何让控件保持固定宽高比?有几种方法供大家选择。
1、自定义view
自定义view,重写onMeasure或onLayout等相关方法,通过预定的比例计算宽高。
下面是简单示例:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int width = MeasureSpec.getSize(widthMeasureSpec);if (mRatio != 0) {float height = width / mRatio;heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY);}super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
这种方法是很多早期开发者喜欢的方式,但是缺点是需要自己重新自定义一个view。
2、adjustViewBounds
为ImageView设置adjustViewBounds,如下:
android:adjustViewBounds="true"
这样ImageView就会以图片的宽高比显示。
但是这个方法的缺点是只能用于ImageView。
3、百分比布局
Android提供了Android-percent-support这个库,支持百分比布局,包括PercentRelativeLayout和PercentFrameLayout。
使用PercentFrameLayout也可以实现一个组件的固定比例显示,代码如下:
<android.support.percent.PercentFrameLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:layout_width="0dp"android:layout_height="0dp"android:scaleType="fitXY"app:layout_widthPercent="100%"app:layout_aspectRatio="@fraction/circle_article_aspectRatio"/></android.support.percent.PercentFrameLayout>
需要在res/values下新建一个fraction.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources><item name="circle_article_aspectRatio" type="fraction">133%</item>
</resources>
这样就实现了宽高4:3的比例。
这个方法的优点是不必自定义view。缺点是组件外层需要包裹一个百分比布局,同时需要一个设置ratio的xml文件。
4、ConstraintLayout
这种方式与百分比布局类似,使用的是ConstraintLayout的DimensionRatio属性,代码如下:
<android.support.constraint.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:layout_width="0dp"android:layout_height="0dp"android:src="@mipmap/bb"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintDimensionRatio="4:3"/>
</android.support.constraint.ConstraintLayout>
这种方法的优点是不用自定义view,相对于百分比布局不需要创建一个设置ratio的xml文件;缺点是需要使用ConstraintLayout。
在上面示例中我们将ImageView的宽高都设置为0。就此我测试了其他的可能性,产生的几个情况如下:
1、如果组件宽高都设置0dp,组件宽高按比例,且只受父view的约束。如图
2、如果其中一个设置成了wrap_content,比如说宽度,那么宽度就会是 图片的真实宽度 和 父view的限制宽度 的较小值,而高度会根据宽度和比例计算出来。
这时如果图片较小,就不会撑满父View。如图
3、
Android中如何使控件保持固定宽高比相关推荐
- android 怎么固定控件,Android中如何使控件保持固定宽高比
我们在android开发过程中可能会遇到一种情况,一个组件需要保持固定的宽高比,但是组件本身大小却不定.尤其在android屏幕碎片化的情况下,很多时候我们需要让一个组件宽度与屏幕宽度一致,这样就无法 ...
- Android中的基础控件TextView、Button、ImageView、EditText、ProgressBar
文章目录 1 Android中的基础控件 1.1 控件的通用属性 2 TextView 2.1 TextView的继承关系 2.2 TextView的常用属性 3 EditText 3.1 常用属性 ...
- Android中进度条控件使用
android中进度条控件使用 ProgressBar pb = findViewById(R.id.pb);pb.setMax(100);pb.setProgress(33); 转载于:https: ...
- 从零开始学android:Android中的基本控件(上)
从零开始学android:Android中的基本控件(上) 本章内容较多,下面只贴代码,大家只需要贴到自己eclipse里就知道作用^^! View组件简介 Android中的View组件包含了几乎所 ...
- Android 布局中 如何使控件居中
首先要分两种不同情况,在两种不同的布局方式下:LinearLayout 和RelativeLayout 1. LinearLayout a). android:layout_gravity=" ...
- android控件位置居中,Android 的布局中如何使控件居中
首先要分两种不同情况,在两种不同的布局方式下:LinearLayout 和RelativeLayout 1. LinearLayout a). android:layout_gravity=" ...
- Android中的常用控件之进度条(ProgressBar)
ProgressBar的常用属性 style(进度条的样式,默认为圆形:用style="?android:attr/progressBarStyleHorizontal"可以将进度 ...
- android中翻页控件,Android GridView控件分页自定义
上一篇:Android GridView控件自定义中,我们自定义了Android GridView控件. 包名解释: com.yaomei.activity.adapter DEMO使用到的自定义 ...
- android 中使用TabHost控件实现微信界面的底部菜单效果
首先,在布局文件中的代码如下:(菜单位于底部,需要在代码中设置) <TabHostandroid:id="@android:id/tabhost"android:layout ...
最新文章
- Nginx + FastCgi + Spawn-fcgi + c 的架构
- Android中Gallery和ImageSwitcher的使用
- HDU 6301.Distinct Values-贪心、构造字典序最小的数列 (2018 Multi-University Training Contest 1 1004)...
- cmd 切换目录_Linux Shell从入门到删除根目录跑路指南
- (不误正业)鼓励做题的时间陷阱
- go语言和python-新学语言,选GO还是Python
- Coding List
- myeclipse中加入jad查看jar源代码
- union中结构体整合后字节对齐问题
- Debian系、红帽系、Arch Linux系如何选择安装包
- 在系统编程ISP及在应用编程IAP
- 一个正经的前端学习 开源 仓库(阶段二十六)
- python爬虫qq好友信息_qq好友空间说说爬虫
- Java连接redis集群报错,connection refused 和Could not get a resource from the pool
- 轻量级openpose解析
- dell 台式电脑设置每天定时开机和关机
- springboot调用第三方邮箱发送邮件过程详解
- 83.Django项目中使用验证码
- 禅道集成聊天工具喧喧,敏捷开发沟通面对面
- Android 之Google认证GMS详细解剖