android百分比布局失效,Android 百分比布局库【原创】
为了解决android手机适配问题,我们经常想如果可以按照百分比的方式进行界面布局,这样适配各种屏幕就简单多了吧!现在谷歌正式提供百分比布局支持库(android-support-percent-lib)。
这个库提供了:
两种布局供大家使用: PercentRelativeLayout、PercentFrameLayout,通过名字就可以看出,这是继承自FrameLayout和RelativeLayout两个容器类;
支持的属性有:
layout_widthPercent、layout_heightPercent、
layout_marginPercent、layout_marginLeftPercent、
layout_marginTopPercent、layout_marginRightPercent、
layout_marginBottomPercent、layout_marginStartPercent、layout_marginEndPercent。
可以看到支持宽高,以及margin。
也就是说,大家只要在开发过程中使用PercentRelativeLayout、PercentFrameLayout替换FrameLayout、RelativeLayout即可。
使用:
使用Android studio在build.gradle添加以下信息就可以获取支持库,当然了,如果你没有下载到该支持库会提示你下载。
PercentRelativeLayout使用小例子:
布局文件如下:
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">
android:id="@+id/row_one_item_one"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:background="#7700ff00"
android:text="w:70%,h:20%"
android:gravity="center"
app:layout_heightPercent="20%"
app:layout_widthPercent="70%"/>
android:id="@+id/row_one_item_two"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_toRightOf="@+id/row_one_item_one"
android:background="#396190"
android:text="w:30%,h:20%"
app:layout_heightPercent="20%"
android:gravity="center"
app:layout_widthPercent="30%"/>
android:id="@+id/row_two_item_one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="centerCrop"
android:layout_below="@+id/row_one_item_one"
android:background="#d89695"
app:layout_heightPercent="70%"/>
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_below="@id/row_two_item_one"
android:background="#770000ff"
android:gravity="center"
android:text="width:100%,height:10%"
app:layout_heightPercent="10%"
app:layout_widthPercent="100%"/>
效果图如下
因为没有LinearLayout我们可以自己来自定义。刚好在网上看到有自定义好的PercentLinearlayout,分享给大家。
public class PercentLinearlayout extends LinearLayout{
private PercentLayoutHelper mPercentLayoutHelper;
public PercentLinearlayout(Context context, AttributeSet attrs)
{
super(context, attrs);
mPercentLayoutHelper = new PercentLayoutHelper(this);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
mPercentLayoutHelper.adjustChildren(widthMeasureSpec, heightMeasureSpec);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mPercentLayoutHelper.handleMeasuredStateTooSmall())
{
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
super.onLayout(changed, l, t, r, b);
mPercentLayoutHelper.restoreOriginalParams();
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs)
{
return new LayoutParams(getContext(), attrs);
}
public static class LayoutParams extends LinearLayout.LayoutParams
implements PercentLayoutHelper.PercentLayoutParams
{
private PercentLayoutHelper.PercentLayoutInfo mPercentLayoutInfo;
public LayoutParams(Context c, AttributeSet attrs)
{
super(c, attrs);
mPercentLayoutInfo = PercentLayoutHelper.getPercentLayoutInfo(c, attrs);
}
@Override
public PercentLayoutHelper.PercentLayoutInfo getPercentLayoutInfo()
{
return mPercentLayoutInfo;
}
@Override
protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr)
{
PercentLayoutHelper.fetchWidthAndHeight(this, a, widthAttr, heightAttr);
}
public LayoutParams(int width, int height) {
super(width, height);
}
public LayoutParams(ViewGroup.LayoutParams source) {
super(source);
}
public LayoutParams(MarginLayoutParams source) {
super(source);
}
}
}
使用效果如下:
参考博客:http://blog.csdn.net/lmj623565791/article/details/46695347
转载时请注明出处及相应链接,本文永久地址:https://blog.yayuanzi.com/13810.html
微信打赏
支付宝打赏
感谢您对作者Lena的打赏,我们会更加努力! 如果您想成为作者,请点我
android百分比布局失效,Android 百分比布局库【原创】相关推荐
- android 底部弹窗失效,Android实现从底部弹出Dialog(和PopWindow实现的效果同样)
布局文件:dialog_custom_layout.xmlandroid android:orientation="vertical" android:layout_width=& ...
- android webview js 失效,Android WebView注入JQuery、JS脚本及执行无效的问题解决
在项目中遇到JQuery注入后,执行无效的问题. 我们知道必须在网页加载完成后,也就是在onPageFinished()方法被调用后才能执行被注入的JS. 但是在有些手机上并不能成功执行,我的解决方案 ...
- android百分比布局失效,Android Studio不能添加百分比布局的依赖,书和AS不一样,老报错,怎么弄?急~~~...
我正好也在看这本书,这里也遇到同样的问题,不过仔细对比书上,可以看到书上的定义有: compile 'com.android.support:appcompat-v7:24.2.1' compile ...
- android点击失效,android点击无效验证的解决方法
背景 在写一个东西滑动删除列表的时候,出现了一个问题.我的需求是,左滑然后出现delete,然后点击delete,让该滑块消失. 我在点列表的第一行的时候,左滑,出现delete,点击删除,ok的,完 ...
- android百分比扩展枯,Android 增强版百分比布局库 为了适配而扩展
一 概述 上周一我们发布了Android 百分比布局库(percent-support-lib) 解析与扩展中对percent-support这个库进行了解析和添加了PercentLinearLayo ...
- android软件百分比怎么实现,Android自实现百分比布局
在开发中,组件布局是大家每日开发必须要面对的工作,对于Android来说提供五种常用布局,分别是:LinearLayout(线性布局) TableLayout(表格布局) RelativeLayout ...
- Android 布局开发之百分比布局、弹性布局
1.百分比布局 很简单,超级简单.引用之后就可以使用了. compile 'com.android.support:percent:23+' git地址: https://github.com/Jul ...
- Android 入门第二讲03-约束布局ConstraintLayout(可视化介绍,Chains链,MATCH_CONSTRAIN,百分比布局,圆形定位,Guideline,Barrier)
Android 入门第二讲03-约束布局ConstraintLayout(可视化介绍,Chains链,MATCH_CONSTRAIN,百分比布局,圆形定位,Guideline,Barrier) 1.可 ...
- Android开发项目实战:实现折叠式布局,android组件化开发与sdk
android:layout_width="match_parent" android:layout_height="200dp"> <androi ...
最新文章
- rabbitmq sleuth zinkip 分布式链路追踪
- List Tuple Dictionary 区别
- 在anaconda中安装tensorflow-GPU版本
- java搭建tcp客户端_【Java学习笔记】TCP客户端/服务器端
- python开发出来的crm系统_用Python打造一个CRM系统(三)
- LeetCode MySQL 1097. 游戏玩法分析 V
- System Center Technical Preview DPM(2016)对Exchange2016的灾难恢复
- win7下搭建opengl es 2.0开发环境
- SpringBoot2.0应用(五):SpringBoot2.0整合MyBatis
- 手把手教你申请Coursera的课程助学金
- 数据管理DMS移动版之2018新年巨献
- 基于特征匹配的英文印刷字符识别代码自己的注释
- java中的mybatis作用_mybatis作用、基本使用、小结
- 2022-2028年中国代理记账行业市场全景调查及投资前景趋势报告
- 视频营销(Video Marketing)1-视频营销基础
- ssh设置免密登录后登录仍需密码可能的原因,以及 ssh 出问题或的调试方法
- 深圳图高智能深耕5G三防夜视智能终端细分领域,引领行业发展
- 百度ai文字转语音并下载php源码,一种基于百度AI的网页版在线语音包转文字的方法与流程...
- 一个人久了,会上瘾的。(转载)
- 淘宝卖家去开拍拍微店真的能成功吗?
热门文章
- presto是如何保证作业内存不会发生冲突和溢出
- 带你尝鲜LiteOS 组件EasyFlash
- 【华为云技术分享】Sketch插件技术 — 让研发和设计高效协同
- 漫谈边缘计算(四):赢家是软还是硬
- 基于Matlab的跨孔电磁波\跨孔雷达的胖射线追踪(一)
- cent6.8 nginx php,CentOS 6.8 安装Nginx
- LaTeX调整目录的行距
- (组合数学笔记)Pólya计数理论_Part.6_置换群的循环指数
- 【numpy】numpy.ones()函数
- 从零开始学keras之卷积神经网络介绍