配置
如果你要使用persentLayout這一個佈局,那麼在舊版本中,你需要compile這個佈局的包
,不過在新版本中最後使用androidx。
可以看下面這篇博客來對比
(https://www.jianshu.com/p/029c1f527135)
需要注意的是,在配置完build後需要進行sync重新設置
PercentFrameLayout
代碼如下
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentFrameLayout 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">
<Button
android:id="@+id/btn1"
android:layout_gravity="left|top"
android:text="btn1"
android:textAllCaps="false"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%" />
<Button
android:layout_gravity="right|top"
android:text="btn2"
android:textAllCaps="false"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"></Button>
<Button
android:layout_gravity="right|bottom"
android:text="btn4"
android:textAllCaps="false"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
></Button>
<Button
android:layout_gravity="left|bottom"
android:text="btn3"
android:textAllCaps="false"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
></Button>
</androidx.percentlayout.widget.PercentFrameLayout>
效果如圖
需要注意的是,我們使用heightPercent這個屬性的時候使用的是app這一個命名空間。
PercentRelativeLayout
xml代碼如下
<?xml version="1.0" encoding="utf-8"?>
<androidx.percentlayout.widget.PercentRelativeLayout 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">
<Button
android:id="@+id/btn1"
android:textAllCaps="false"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"></Button>
<Button
android:layout_below="@id/btn1"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
></Button>
<Button
android:layout_toRightOf="@id/btn1"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
></Button>
<Button
android:layout_below="@id/btn1"
app:layout_heightPercent="50%"
app:layout_widthPercent="50%"
android:layout_toRightOf="@id/btn1"
></Button>
</androidx.percentlayout.widget.PercentRelativeLayout>
效果與PercentFrameLayout效果一樣