写给初学者22_android_百分比布局
来源:互联网 发布:js qq客服代码 编辑:程序博客网 时间:2024/06/11 08:11
安卓22_百分比布局
日常开发中我们使用LinearLayout来进行开发布局,并且活用layout-weight属性会为我们的开发节省很多得时间,并且一定程度上解决了开发中的适配问题。但是LinearLayout的局限在于我们只能有两个方向上的摆放,不怎么灵活。这个时候我们会去怀念RelativeLayout。不过RelativeLayout却没有layout-weight这样的布局参数可以设置。确实很让人头疼,这个时候SDK提出了一种全新的布局方式,百分比布局。
分为两种
- PercentFrameLayout
- PercentRelativeLayout
可以看到几乎只是在帧布局和相对布局上做了扩展。
使用
这里以PercentRelativeLayout为例。
添加依赖
在APP模块下添加一个依赖。
compile 'com.android.support:percent:23.4.0'
至于版本根据合适选择就好。添加后需要同步一下项目。
直接使用
<?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"android:id="@+id/activity_main"android:layout_width="match_parent"android:layout_height="match_parent"><TextView android:layout_centerInParent="true" android:background="#2d7d9a" android:gravity="center" android:text="Center" app:layout_heightPercent="50%" app:layout_widthPercent="50%" /><TextView android:background="#2d7d9a" android:gravity="center" android:text="left|top" app:layout_heightPercent="25%" app:layout_widthPercent="25%" /><TextView android:layout_alignParentRight="true" android:background="#2d7d9a" android:gravity="center" android:text="right|top" app:layout_heightPercent="25%" app:layout_widthPercent="25%" /><TextView android:layout_alignParentBottom="true" android:background="#2d7d9a" android:gravity="center" android:text="left|bottom" app:layout_heightPercent="25%" app:layout_widthPercent="25%" /><TextView android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="#2d7d9a" android:gravity="center" android:text="right|bottom" app:layout_heightPercent="25%" app:layout_widthPercent="25%" /></android.support.percent.PercentRelativeLayout>
注意点
- 写出完整的包名
- 需要声明一个命名空间
- 使用的是layout_widthPercent属性
- 如果AndroidStudio版本比较低是无法预览的,但是不影响结果
结果
这里简单的使用就是确定宽和高的比例,但是可以看出功能已经非常强大了,结合了RelativeLayout的特点和比例特点。所在这当中RelativeLayout的属性特点你依旧可以继续爽快的应用。
布局参数
- layout_widthPercent控件宽度为父容器的宽的百分比
- layout_heightPercent控件高度为父容器的高的百分比
- layout_marginPercent
- layout_marginLeftPercent控件与左边控件的距离为父容器的宽度的百分比
- layout_marginTopPercent控件与上方控件的距离为父容器的高度的百分比
- layout_marginRightPercent控件与右边控件的距离为父容器的宽度的百分比
- layout_marginBottomPercent控件与下方控件的距离为父容器的高度的百分比
0 0
- 写给初学者22_android_百分比布局
- 写给初学者02_android_常用工具
- 写给初学者01_android_什么是安卓
- 写给初学者03_android_第一个安卓程序
- 写给初学者03_android_第一个安卓程序
- 写给初学者14_android_界面代码优化
- 写给初学者16_android_常用控件_ListView
- 写给初学者17_Android_四大组件-Activity
- 写给初学者18_android_四大组件_Service
- 写给初学者20_android_四大组件_BroadCastReciver
- 写给初学者04_android_安卓的视图
- 写给初学者08_android_一些常用的控件
- 写给初学者09_android_效果标签_shape_形状标签
- 写给初学者12_android_界面总结_初步
- 写给初学者15_android_基本的用户交互-点击响应
- 写给初学者19_android_四大组件_ContentProvider初级
- 写给初学者23_android_数据持久化_文件
- 写给初学者25_android_数据持久化_slqlite
- C++ 和lua交互学习的三个例子
- (c语言)在一个有数字的字符串中输出最长的数字
- PRML读书笔记——线性回归模型
- c++运算符重载的方法提要
- 选项卡控制器(tabBarController)基础
- 写给初学者22_android_百分比布局
- 二分图的最大匹配——匈牙利算法
- 【面试题】剑指Offer-50-求两个节点的最近公共祖先
- 二叉树的基本操作(严蔚敏)
- 深入理解LayoutInflater.inflate()
- JVM内存模型
- redis高级实用特性
- Python针对特定服务定制的代理工具V2.0------(zipdeal.py)
- Android--推送机制实现原理(四)-自己实现推送功能--心跳包