使用前需要在app/build.gradle文件中添加如下内容:
compile 'com.android.support:percent:25.1.1'
注意25.1.1为版本,有要求,和上面版本填一样。
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.1'
compile 'com.android.support:percent:25.1.1'
testCompile 'junit:junit:4.12'
}
修改app/build.gradle文件时,会提示:
Gradle files have changed since last project sync. A project sync may be necessary for the IDE to work properly. Sync Now
点击Sync Now即可。
基本配置如下:
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/button1"
android:text="Button"
android:layout_gravity="left|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%" />
<Button
android:id="@+id/button2"
android:text="Button"
android:layout_gravity="right|top"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%" />
<Button
android:id="@+id/button3"
android:text="Button"
android:layout_gravity="left|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%" />
<Button
android:id="@+id/button4"
android:text="Button"
android:layout_gravity="right|bottom"
app:layout_widthPercent="50%"
app:layout_heightPercent="50%" />
</android.support.percent.PercentFrameLayout>