Android ConstraintLayout按比例缩放View
Android ConstraintLayout按比例缩放View
关键点有两个,第一,使用Android ConstraintLayout的layout_constraintDimensionRatio属性,设置宽高比缩放比例,宽:高。第二,恒定宽和高,另外一个方向就会按照比例缩放。
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/text0"
android:layout_width="0dp"
android:layout_height="100dp"
android:background="@android:color/holo_orange_light"
android:gravity="center"
android:text="w=0,h=100\n5:2"
android:textColor="@android:color/white"
android:textSize="10dp"
app:layout_constraintDimensionRatio="5:2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text1"
android:layout_width="100dp"
android:layout_height="0dp"
android:background="@android:color/holo_red_light"
android:gravity="center"
android:text="w=100,h=0\n5:2"
android:textColor="@android:color/white"
android:textSize="10dp"
app:layout_constraintDimensionRatio="5:2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@id/text0" />
</android.support.constraint.ConstraintLayout>
运行结果如图:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
12.源码阅读(app启动流程-android api 26)
activity的启动流程之前已经通过源码了解了,那么app的启动流程是怎样的,从我们按下app的图标,到应用启动起来显示出画面,中间都经历了什么? 安卓是基于java的,所以和java有一定的相似性,一个java程序的起点是main,那么android是不是也是如此?事实上,正如猜想我们找到ActivityTread这个类中的main方法,这里就是app启动的源头 public static void main(String[] args) { Trace.traceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, "ActivityThreadMain"); SamplingProfilerIntegration.start(); CloseGuard.setEnabled(false); //初始化内存卡环境 Environment.initForCurrentUser(); // Set the reporter for event logging in libcore EventLogger.setReporter(new EventLoggin...
-
下一篇
Android测量View宽和高的一般通用方法
Android测量View宽和高的一般通用方法 众所周知Android在onCreate里面如果直接对一个View进行getWidth,getHeight时候,得到到的值全部是0,这样拿不到View的宽和高的值,但有时候又必须在初始化阶段获取一个View的宽和高,现在给出一个基本的通用方法: int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); mAndroidView.measure(w, h); int width = text.getMeasuredWidth(); int height = text.getMeasuredHeight(); mAndroidView即为一般的Android View。 最终测算出来的width和height均为pix值。
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Docker安装Oracle12C,快速搭建Oracle学习环境
- CentOS7,CentOS8安装Elasticsearch6.8.6
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- CentOS8编译安装MySQL8.0.19
- MySQL数据库在高并发下的优化方案
- CentOS6,7,8上安装Nginx,支持https2.0的开启
- MySQL8.0.19开启GTID主从同步CentOS8
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS7,8上快速安装Gitea,搭建Git服务器