Android屏幕適配之百分比布局

為什么使用百分比布局

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

如何使用百分比布局

1.添加依賴

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

2.屬性講解

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

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

這里由于不設置layout_width和layout_height的話Android Studio會報錯所以我們把它設置成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>
效果圖

個人技術博客:https://myml666.github.io

最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末喳整,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子裸扶,更是在濱河造成了極大的恐慌框都,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件呵晨,死亡現(xiàn)場離奇詭異魏保,居然都是意外死亡,警方通過查閱死者的電腦和手機摸屠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門谓罗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人餐塘,你說我怎么就攤上這事妥衣。” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵税手,是天一觀的道長蜂筹。 經(jīng)常有香客問我,道長芦倒,這世上最難降的妖魔是什么艺挪? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮兵扬,結(jié)果婚禮上麻裳,老公的妹妹穿的比我還像新娘。我一直安慰自己器钟,他們只是感情好津坑,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著傲霸,像睡著了一般疆瑰。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上昙啄,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天穆役,我揣著相機與錄音,去河邊找鬼梳凛。 笑死耿币,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的韧拒。 我是一名探鬼主播淹接,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼叭莫!你這毒婦竟也來了蹈集?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤雇初,失蹤者是張志新(化名)和其女友劉穎拢肆,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體靖诗,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡郭怪,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了刊橘。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片鄙才。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖促绵,靈堂內(nèi)的尸體忽然破棺而出攒庵,到底是詐尸還是另有隱情嘴纺,我是刑警寧澤,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布浓冒,位于F島的核電站栽渴,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏稳懒。R本人自食惡果不足惜闲擦,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望场梆。 院中可真熱鬧墅冷,春花似錦、人聲如沸或油。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽顶岸。三九已至罐脊,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間蜕琴,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工宵溅, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留凌简,地道東北人。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓恃逻,卻偏偏與公主長得像雏搂,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子寇损,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354