Android屏幕適配之百分比布局

為什么使用百分比布局

由于Android系統(tǒng)的碎片化發(fā)展導(dǎo)致了市面上多種分辨率、多種屏幕密度共存,這對(duì)我們的屏幕適配增加了不少的難度,在布局方面我們都知道可以通過(guò)LinearLayout的layout_weight屬性來(lái)進(jìn)行適配,但是在某些情況下我們要向用這種方法進(jìn)行適配就必須進(jìn)行多層布局嵌套,而這則會(huì)導(dǎo)致布局文件復(fù)雜,增加渲染層次,致使性能下降。針對(duì)這種情況google為我們提供了一個(gè)百分比布局兼容庫(kù):Android Percent Support Library,解決了上述的問(wèn)題,目前它支持RelativeLayout和FrameLayout的百分比布局,不過(guò)已經(jīng)有大牛在GitHub上面開(kāi)源了LinearLayout的百分比布局支持庫(kù)。

如何使用百分比布局

1.添加依賴(lài)

dependencies {
    ...
    implementation 'com.android.support:percent:27.0.2'
}

2.屬性講解

在函數(shù)庫(kù)里面我們主要用到兩個(gè)類(lèi):
  • PercentRelativeLayout
  • 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:用百分比表示距離第一個(gè)View之間的距離
  • app:layout_marginEndPercent:用百分比表示距離最后一個(gè)View之間的距離
  • app:layout_aspectRatio:用百分比表示View的寬高比

3.實(shí)例演示(以PercentRelativeLayout為例)

這里由于不設(shè)置layout_width和layout_height的話(huà)Android Studio會(huì)報(bào)錯(cuò)所以我們把它設(shè)置成0dp。
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/one"
        android:background="#f0f000"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="70%"
        android:text="Hello World!"
         />
    <TextView
        android:layout_width="0dp"
        android:layout_toRightOf="@id/one"
        android:layout_height="0dp"
        app:layout_heightPercent="30%"
        app:layout_widthPercent="30%"
        android:background="#ff0000"
        android:text="Hello World!"
        />
    <TextView
          android:layout_width="0dp"
          android:layout_below="@id/one"
          android:layout_height="0dp"
          app:layout_heightPercent="70%"
          app:layout_widthPercent="100%"
          android:background="#ff00ff"
          android:text="Hello World!"
            />

</android.support.percent.PercentRelativeLayout>
效果圖
layout_aspectRatio屬性演示
<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <TextView
        android:layout_width="100dp"
        android:layout_height="0dp"
        app:layout_aspectRatio="50%"
        android:background="#ff00ff"
        android:text="Hello World!"
        />
    <TextView
        android:layout_width="100dp"
        android:layout_height="0dp"
        android:layout_alignParentRight="true"
        app:layout_aspectRatio="200%"
        android:background="#0f00ff"
        android:text="Hello World!"
        />
</android.support.percent.PercentRelativeLayout>
效果圖

個(gè)人技術(shù)博客:https://myml666.github.io

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容