Android support Percent supports Library Development and androidpercent
The layout of Android supports percent setting for development to learn how to implement it. However, it looks like a webpage setting. For example, the setting attribute of width is 'layout _ widthPercent '. Before that, we usually set the weight of Linearlayout to realize the ratio between layout S.
Percent support Library provides two new classes:
1. PercentRelativeLayout
2. PercentFrameLayout
Create a new project
Create a new project for testing. Modify 'build. gradle' and introduce the following libraries:
`applyplugin:'com.android.application'android {compileSdkVersion23buildToolsVersion"23.0.0"defaultConfig {applicationId"com.android.chaowen.percentdemo1"minSdkVersion7targetSdkVersion22versionCode1versionName"1.0"}buildTypes {release {minifyEnabledfalseproguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'}}}dependencies {compile fileTree(dir:'libs',include: ['*.jar'])compile'com.android.support:support-annotations:23.0.0'compile'com.android.support:appcompat-v7:23.0.0'compile'com.android.support:design:23.0.0'compile'com.android.support:percent:23.0.0'}
Create Layout
xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity">android:id="@+id/first"android:background="@color/sa_green_dark"app:layout_heightPercent="50%"app:layout_marginLeftPercent="25%"app:layout_marginTopPercent="25%"app:layout_widthPercent="50%" />android:layout_width="0dp"android:layout_height="32dp"android:layout_alignLeft="@id/first"android:layout_alignStart="@id/first"android:layout_alignRight="@id/first"android:layout_alignEnd="@id/first"android:layout_below="@id/first"android:layout_marginTop="8dp"android:background="@color/light_grey" />
The percentage value is set through the 'heightcpercent 'and 'widthpercent' attributes. Completely belongs to the extension class of 'relativelayout'. It is worth mentioning that,
You do not need to set 'layout _ width' or 'layout _ height'. Note that this is a new library usage method because these two attributes are automatically added.
Baidu ratio can also be used to set margins. The only difference is that the percentage value is adopted.
At last, you must note that in the second view, the proportional size is not directly set, but its position is relative to the first view.