这个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
- <android.support.percent.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">
-
- <View
- android:id="@+id/top_left"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_alignParentTop="true"
- android:background="#ff44aacc"
- app:layout_heightPercent="20%"
- app:layout_widthPercent="70%" />
- <View
- android:id="@+id/top_right"
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:layout_alignParentTop="true"
- android:layout_toRightOf="@+id/top_left"
- android:background="#ffe40000"
- app:layout_heightPercent="20%"
- app:layout_widthPercent="30%" />
- <View
- android:id="@+id/bottom"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_below="@+id/top_left"
- android:background="#ff00ff22"
- app:layout_heightPercent="80%" />
- </android.support.percent.PercentRelativeLayout>
PercentLinearLayout
- <com.juliengenoud.percentsamples.PercentLinearLayout
- 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:orientation="vertical">
- <View
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:background="#ff44aacc"
- app:layout_heightPercent="10%"
- app:layout_widthPercent="60%"/>
-
- <View
- android:layout_width="0dp"
- android:layout_height="0dp"
- android:background="#ff4400cc"
- app:layout_heightPercent="10%"
- app:layout_widthPercent="70%"/>
- </com.juliengenoud.percentsamples.PercentLinearLayout>
PercentFrameLayout
- <android.support.percent.PercentFrameLayout
- 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">
- <!-- ... XML CODE -->
- </android.support.percent.PercentFrameLayout>