Android開發(fā)學習教程(16)- Android布局之百分比布局PercentLayout
—— 世之奇?zhèn)?、瑰怪、非常之觀,常在于險遠,而人之所罕至焉,故非有志者不能至也。
上一篇我們講了幀布局FrameLayout的基本用法,這里來學習常用布局之百分比布局PercentLayout的基本用法。
百分比布局是什么
百分比布局中顧名思義就是按照百分比固定控件的長或寬。百分比布局大致可分為三種,一種是我們上兩篇已經(jīng)學過的線性布局LinearLayout,它可以通過設置子控件的layout_weight屬性來實現(xiàn)百分比效果;第二三種是谷歌提供的支持庫PercentFrameLayout和PercentRelativeLayout;
百分比布局有什么用
按照百分比固定控件的長或寬,對適配不同分辨率手機有一定幫助。
百分布局怎么用
第一種百分比布局LinearLayout
繼續(xù)基于上一篇的項目,我們新建一個LinearLayout:
<?
xml
?version
=
"1.0"
?encoding
=
"utf-8"
?>
<
LinearLayout
?xmlns:android
=
"http://schemas.android.com/apk/res/android"
????
android:layout_width
=
"match_parent"
????
android:layout_height
=
"match_parent"
????
android:orientation
=
"horizontal"
>
????
<
TextView
????????
android:layout_width
=
"0dp"
????????
android:layout_height
=
"40dp"
????????
android:layout_weight
=
"1"
????????
android:background
=
"@android:color/holo_red_light"
?/>
????
<
TextView
????????
android:layout_width
=
"0dp"
????????
android:layout_height
=
"40dp"
????????
android:layout_weight
=
"2"
????????
android:background
=
"@android:color/holo_green_light"
?/>
????
<
TextView
????????
android:layout_width
=
"0dp"
????????
android:layout_height
=
"40dp"
????????
android:layout_weight
=
"3"
????????
android:background
=
"@android:color/holo_blue_bright"
?/>
</
LinearLayout
>
首先布局是線性布局LinearLayout,屬性android:orientation=”horizontal”,則所有子控件橫向排列。第一個子控件android:layout_width=”0dp”、android:layout_weight=”1″則是控件的寬度不固定,占父控件LinearLayout的1/6;第二個子控件android:layout_width=”0dp”、android:layout_weight=”2″則是控件的寬度不固定,占父控件LinearLayout的2/6;第三個子控件android:layout_width=”0dp”、android:layout_weight=”3″則是控件的寬度不固定,占父控件LinearLayout的3/6;
沒有設置任何屬性,控件默認顯示在FrameLayout左上角
PercentFramLayout是谷歌提供的支持庫,使用之前需在項目的build.gradle文件引入支持庫
implementation "androidx.percentlayout:percentlayout:1.0.0"
然后我們新建一個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"
????
android:layout_width
=
"match_parent"
????
android:layout_height
=
"match_parent"
>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:background
=
"@android:color/holo_red_light"
????????
app:layout_heightPercent
=
"10%"
????????
app:layout_widthPercent
=
"10%"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:background
=
"@android:color/holo_green_light"
????????
app:layout_heightPercent
=
"20%"
????????
app:layout_marginLeftPercent
=
"10%"
????????
app:layout_widthPercent
=
"20%"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:background
=
"@android:color/holo_blue_bright"
????????
app:layout_heightPercent
=
"30%"
????????
app:layout_marginLeftPercent
=
"30%"
????????
app:layout_widthPercent
=
"30%"
?/>
</
androidx.percentlayout.widget.PercentFrameLayout
>
首先布局是PercentFrameLayout,顧名思義是一個具有百分比屬性的FrameLayout。因為布局FrameLayout不能像LinearLayout的子控件一樣通過layout_weight來設置子控件的百分比,所以谷歌提供了PercentFrameLayout布局來實現(xiàn)基于FrameLayout的百分比布局。當布局設置為PercentFrameLayout的時候,子控件有如下屬性:
app:layout_heightPercent:用百分比表示高度
app:layout_widthPercent:用百分比表示寬度
app:layout_marginPercent:用百分比表示View之間的間隔
app:layout_marginLeftPercent:用百分比表示左邊間隔
app:layout_marginRight:用百分比表示右邊間隔
app:layout_marginTopPercent:用百分比表示頂部間隔
app:layout_marginBottomPercent:用百分比表示底部間隔
app:layout_marginStartPercent:用百分比表示距離第一個View之間的距離
app:layout_marginEndPercent:用百分比表示距離最后一個View之間的距離
app:layout_aspectRatio:用百分比表示View的寬高比
先來看第一個子控件
<
TextView
????
android:layout_width
=
"wrap_content"
????
android:layout_height
=
"wrap_content"
????
android:background
=
"@android:color/holo_red_light"
????
app:layout_heightPercent
=
"10%"
????
app:layout_widthPercent
=
"10%"
?/>
表示控件TextView的高度占PercentFrameLayout高度的10%,寬度占PercentFrameLayout寬度的10%
第二個子控件
<
TextView
????
android:layout_width
=
"wrap_content"
????
android:layout_height
=
"wrap_content"
????
android:background
=
"@android:color/holo_green_light"
????
app:layout_heightPercent
=
"20%"
????
app:layout_marginLeftPercent
=
"10%"
????
app:layout_widthPercent
=
"20%"
?/>
表示控件TextView的高度占PercentFrameLayout高度的20%,寬度占PercentFrameLayout寬度的20%,距離PercentFrameLayout左邊的距離是PercentFrameLayout寬度的10%,同理,第三個控件TextView的高度占PercentFrameLayout高度的30%,寬度占PercentFrameLayout寬度的30%,距離PercentFrameLayout左邊的距離是PercentFrameLayout寬度的30%。
第三種百分比布局PercentRelativeLayout,我們再來用布局PercentRelativeLayout 實現(xiàn)上面的效果,因為PercentRelativeLayout也是繼承于RelativeLayout。所以結合PercentRelativeLayout的屬性和RelativeLayout屬性其實也很簡單,如下:
<?
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"
????
android:layout_width
=
"match_parent"
????
android:layout_height
=
"match_parent"
>
????
<
TextView
????????
android:id
=
"@+id/tv1"
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:background
=
"@android:color/holo_red_light"
????????
app:layout_heightPercent
=
"10%"
????????
app:layout_widthPercent
=
"10%"
?/>
????
<
TextView
????????
android:id
=
"@+id/tv2"
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:layout_toRightOf
=
"@+id/tv1"
????????
android:background
=
"@android:color/holo_green_light"
????????
app:layout_heightPercent
=
"20%"
????????
app:layout_widthPercent
=
"20%"
?/>
????
<
TextView
????????
android:layout_width
=
"wrap_content"
????????
android:layout_height
=
"wrap_content"
????????
android:layout_toRightOf
=
"@+id/tv2"
????????
android:background
=
"@android:color/holo_blue_bright"
????????
app:layout_heightPercent
=
"30%"
????????
app:layout_widthPercent
=
"30%"
?/>
</
androidx.percentlayout.widget.PercentRelativeLayout
>
源碼鏈接:https://yunjunet.cn/876768.html