Android屏幕适配 超级解决方案percent-support-lib



   这个percent-support-lib包怎么解决屏幕适配的?他提供了按照百分比来作为基础布局的尺寸的参数,避免各种屏幕大小,像素密度的适配的烦心事。下面直接来看看吧。

   Android studio直接加入以下代码

            dependencies {
                  compile 'com.android.support:percent:22.0.0'
            }

   加不成功的打开SDK Manager,将extras更新到最新版本。这个包引入完成后,有了2个新的布局文件可以用,分别是PercentFrameLayout,PercentRelativeLayout。是不是感觉少了点什么,是的,官方的lib中没PercentLinearLayout。不过没关系,大神在民间,Git上有个现成的项目可以用https://github.com/JulienGenoud/android-percent-support-lib-sample

    说说用法,其实很简单。贴上三个例子供参考:

PercentRelativeLayout

[java]  view plain  copy
 print ?
  1. <android.support.percent.PercentRelativeLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.   
  7.     <View  
  8.         android:id="@+id/top_left"  
  9.         android:layout_width="0dp"  
  10.         android:layout_height="0dp"  
  11.         android:layout_alignParentTop="true"  
  12.         android:background="#ff44aacc"  
  13.         app:layout_heightPercent="20%"  
  14.         app:layout_widthPercent="70%" />  
  15.     <View  
  16.         android:id="@+id/top_right"  
  17.         android:layout_width="0dp"  
  18.         android:layout_height="0dp"  
  19.         android:layout_alignParentTop="true"  
  20.         android:layout_toRightOf="@+id/top_left"  
  21.         android:background="#ffe40000"  
  22.         app:layout_heightPercent="20%"  
  23.         app:layout_widthPercent="30%" />  
  24.     <View  
  25.         android:id="@+id/bottom"  
  26.         android:layout_width="match_parent"  
  27.         android:layout_height="0dp"  
  28.         android:layout_below="@+id/top_left"  
  29.         android:background="#ff00ff22"  
  30.         app:layout_heightPercent="80%" />  
  31. </android.support.percent.PercentRelativeLayout>  

PercentLinearLayout

[java]  view plain  copy
 print ?
  1. <com.juliengenoud.percentsamples.PercentLinearLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent"  
  6.     android:orientation="vertical">  
  7.      <View  
  8.         android:layout_width="0dp"  
  9.         android:layout_height="0dp"  
  10.         android:background="#ff44aacc"  
  11.         app:layout_heightPercent="10%"  
  12.         app:layout_widthPercent="60%"/>  
  13.   
  14.     <View  
  15.         android:layout_width="0dp"  
  16.         android:layout_height="0dp"  
  17.         android:background="#ff4400cc"  
  18.         app:layout_heightPercent="10%"  
  19.         app:layout_widthPercent="70%"/>  
  20. </com.juliengenoud.percentsamples.PercentLinearLayout>  

PercentFrameLayout
[java]  view plain  copy
 print ?
  1. <android.support.percent.PercentFrameLayout  
  2.     xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     android:layout_width="match_parent"  
  5.     android:layout_height="match_parent">  
  6.         <!-- ... XML CODE -->  
  7. </android.support.percent.PercentFrameLayout>  

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值