Android的佈局支持百分比的設置進行開發,來學習如何去實現它,不過看起來會像網頁的設置,好比寬度的設置屬性是`layout_widthPercent`。在此以前,咱們通常都會設置Linearlayout的weight權重來實現佈局間的比例大小。java
Percent support Library提供了兩個新的類:android
1.PercentRelativeLayoutapp
2.PercentFrameLayout佈局
建立一個新的項目來測試,修改`build.gradle`,須要引入如下庫學習
`applyplugin:'com.android.application' android { compileSdkVersion23 buildToolsVersion"23.0.0" defaultConfig { applicationId"com.android.chaowen.percentdemo1" minSdkVersion7 targetSdkVersion22 versionCode1 versionName"1.0" } buildTypes { release { minifyEnabledfalse proguardFilesgetDefaultProguardFile('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' }
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" />
比例大小是經過`heightPercent`和`widthPercent`屬性來設置百分比大小值。徹底屬於`RelativeLayout`的擴展類,值得一提的是,測試
再也不須要設置`layout_width`和`layout_height`,注意了,這是新的庫使用方法,由於這兩個屬性會被自動加入了。gradle
百度比也能夠用於設置邊距。惟一區別的只是採起了百分比值。ui
最後一點須要注意,在第二個view,並無直接設置比例的大小,可是它的位置是相對於第一個view.spa