百分比布局可以让Relativelayout 和FrameLayout 来实现让;两个按钮按比例布局的效果。
百分比布局是为FrameLayout和RelativeLayout进行了功能的扩展,
提供了PercentRelativeLayout 和 PercentFramelayout这两个全新的布局。
百分比布局属于新增布局,其定义在support库当中,我们在引用的时候需要在项目的build.gradle中添加依赖库:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
compile 'com.android.support:percent:24.2.1'
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<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">
<Button
android:id="@+id/bt01"
android:layout_alignParentLeft="true"
app:layout_widthPercent="60%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/bt02"
android:layout_alignParentRight="true"
app:layout_widthPercent="38%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/bt03"
android:layout_alignParentLeft="true"
android:layout_below="@+id/bt01"
app:layout_widthPercent="60%"
app:layout_heightPercent="50%"/>
<Button
android:id="@+id/bt04"
android:layout_alignParentRight="true"
android:layout_below="@+id/bt01"
app:layout_widthPercent="38%"
app:layout_heightPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
这个错误是Android sdudio中内置布局检查机制引起的,这个错误不会影响程序的运行,我们直接忽视就可以了
运行程序,效果如图: