Android屏幕适配之百分比布局
为什么使用百分比布局
由于Android系统的碎片化发展导致了市面上多种分辨率、多种屏幕密度共存,这对我们的屏幕适配增加了不少的难度,在布局方面我们都知道可以通过LinearLayout的layout_weight属性来进行适配,但是在某些情况下我们要向用这种方法进行适配就必须进行多层布局嵌套,而这则会导致布局文件复杂,增加渲染层次,致使性能下降。针对这种情况google为我们提供了一个百分比布局兼容库:Android Percent Support Library,解决了上述的问题,目前它支持RelativeLayout和FrameLayout的百分比布局,不过已经有大牛在GitHub上面开源了LinearLayout的百分比布局支持库。
如何使用百分比布局
1.添加依赖
dependencies { ... implementation 'com.android.support:percent:27.0.2' }
2.属性讲解
在函数库里面我们主要用到两个类:
- PercentRelativeLayout
- PercentFrameLayout
它们主要有以下属性
- app:layout_heightPercent:用百分比表示高度
- app:layout_widthPercent:用百分比表示宽度
- app:layout_marginPercent:用百分比表示View之间的间隔
- app:layout_marginLeftPercent:用百分比表示左边间隔
- app:layout_marginRight:用百分比表示右边间隔
- app:layout_marginTopPercent:用百分比表示顶部间隔
- app:layout_marginBottomPercent:用百分比表示底部间隔
- app:layout_marginStartPercent:用百分比表示距离第一个View之间的距离
- app:layout_marginEndPercent:用百分比表示距离最后一个View之间的距离
- app:layout_aspectRatio:用百分比表示View的宽高比
3.实例演示(以PercentRelativeLayout为例)
这里由于不设置layout_width和layout_height的话Android Studio会报错所以我们把它设置成0dp。
<?xml version="1.0" encoding="utf-8"?> <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="0dp" android:layout_height="0dp" android:id="@+id/one" android:background="#f0f000" app:layout_heightPercent="30%" app:layout_widthPercent="70%" android:text="Hello World!" /> <TextView android:layout_width="0dp" android:layout_toRightOf="@id/one" android:layout_height="0dp" app:layout_heightPercent="30%" app:layout_widthPercent="30%" android:background="#ff0000" android:text="Hello World!" /> <TextView android:layout_width="0dp" android:layout_below="@id/one" android:layout_height="0dp" app:layout_heightPercent="70%" app:layout_widthPercent="100%" android:background="#ff00ff" android:text="Hello World!" /> </android.support.percent.PercentRelativeLayout>
效果图
layout_aspectRatio属性演示
<?xml version="1.0" encoding="utf-8"?> <android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="100dp" android:layout_height="0dp" app:layout_aspectRatio="50%" android:background="#ff00ff" android:text="Hello World!" /> <TextView android:layout_width="100dp" android:layout_height="0dp" android:layout_alignParentRight="true" app:layout_aspectRatio="200%" android:background="#0f00ff" android:text="Hello World!" /> </android.support.percent.PercentRelativeLayout>
效果图
个人技术博客:https://myml666.github.io

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Android中compileSdkVersion、buildToolsVersion、minSdkVersion和targetSdkVer...
AndroidStudio中的build.gradle中包含了这些值,compileSdkVersion和minSdkVersion还比较通俗易懂,compileSdkVersion表示当前项目的编译版本,即你想让AndroidStudio以什么SDK版本去编译你的项目,minSdkVersion表示当前项目支持的最低安卓系统版本,buildToolsVersion则是指构建工具的SDK版本,也就是AndroidStudio中设置的AndroidSDK版本,buildToolsVersion要高于compileSdkVersion版本,这个很好理解,如果编译版本高于AndroidStudio的SDK版本,那AndroidStudio怎么给项目编译呢?targetSdkVersion的意思是当前app的目标sdk版本,可以理解为当前app在这个版本是比较稳定的,针对这个版本所开发的,是安卓提供的前向兼容的主要手段,它的作用就是当targetSdkVersion为20的app运行在系统版本号高于20的手机系统中,app所执行的旧版本api在高版本可能不一样了,但是安卓为了兼容在系统层会读取...
- 下一篇
如何解决 Android7.0之后部分手机无法抓包
最近,测试提出来这么一个问题,说公司android7.0以上的手机没办法抓包,他们拿不到数据。很是尴尬。 难道是因为android7.0近两年兴起?还是因为以前的测试就根本没抓包,,, 好了进入正题,通过一波的查阅资料过后发现:android7.0+的版本新增了证书验证,https的安全证书手机端和电脑端都必须安装,这个是前提;又由于android手机多厂商系统定制的问题,可能有部分手机确实无法抓包,这里,对于抓包的问题上,我们在给测试打包的时候要对我们的项目进行添加设置: 默认新任所有证书。 这个操作其实是比较敏感的,线上不能带上这个配置;下面来说一下相关配置项: 1、在res-xml目录中创建一个名为 “network_security_config.xml”的文件夹:并且附上如下内容: <network-security-config> <base-config cleartextTrafficPermitted="true"> <trust-anchors> <certificates src="system" overridePins...
相关文章
文章评论
共有0条评论来说两句吧...