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

Posted 冰糖葫芦三剑客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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>  

以上是关于Android屏幕适配 超级解决方案percent-support-lib的主要内容,如果未能解决你的问题,请参考以下文章

简述Android 百分比布局支持库(android-percent-support)

Android 屏幕适配:最全面的解决方案

Android开发:最全面最易懂的Android屏幕适配解决方案

Android 屏幕适配方案

Android最全面最易懂的Android屏幕适配解决方案

Android最全面最易懂的Android屏幕适配解决方案