Android 左右均分布局的實(shí)現(xiàn)

本文使用 Android 提供的幾種已有 Layout绰寞,實(shí)現(xiàn)了左右均分的布局效果归苍,并且在實(shí)現(xiàn)的過程中拥峦,對 Android 內(nèi)建布局 LinearLayout只恨、RelativeLayout 等常用布局的使用進(jìn)行了溫習(xí)译仗,將一些以前忽略的細(xì)節(jié)進(jìn)行了回顧抬虽;對近些年新推出的百分比布局、ConstraintLayout 的基本使用進(jìn)行了歸納總結(jié)纵菌;熟悉了一下 FlexboxLayout 的基本使用阐污。

  1. 最熟悉的便是利用 LinearLayout 的 weight 屬性進(jìn)行布局,實(shí)現(xiàn)如下
<?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="100dp"
    android:orientation="horizontal">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/darker_gray" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@android:color/background_dark" />

</LinearLayout>

概括來講咱圆,線性布局就是笛辟,能夠使其子試圖在垂直或水平方向依次堆疊起來的布局,weight 屬性的作用是表明該子視圖所占的比例序苏。如上绢掰,左側(cè)和右側(cè)的視圖均將
weight 的值設(shè)置為了1灿里,二者所占權(quán)重相同瓶颠,因此將均分父容器的所有空間匾浪。

  1. 其實(shí)使用 RelativeLayout 也可是實(shí)現(xiàn)同樣的效果

使用一個尺寸為0的 view 作為 divider,然后讓兩個組件分別居于其左側(cè)和右側(cè)即可匈睁。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <View
        android:id="@+id/divider"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_toLeftOf="@id/divider"
        android:layout_toStartOf="@id/divider"
        android:background="@android:color/darker_gray" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_toEndOf="@id/divider"
        android:layout_toRightOf="@id/divider"
        android:background="@android:color/background_dark" />

</RelativeLayout>
  1. 同樣的监透,使用 RelativeLayout 實(shí)現(xiàn),但是細(xì)節(jié)有些差異软舌。

該實(shí)現(xiàn)與例二幾乎一樣才漆,都是使用 RelativeLayout 以及零尺寸的 divider 來實(shí)現(xiàn)的,該例主要是為了明確一個之前未曾留意過的細(xì)節(jié)佛点,即 RelativeLayout 的 layout_alignXxx 屬性與 layout_toXxxOf 屬性的差異。

  • android:layout_alignRight="@id/divider"
    • 其作用是“對齊”黎比,因此聲明該屬性的 view超营,其右邊界與 divider 的右邊界對齊
  • android:layout_toRightOf="@id/divider"
    • 其作用是“規(guī)定相對位置”,因此聲明該屬性的 view阅虫,其位置在 divider 右側(cè)演闭,并且左邊界與 divider 的右邊界對齊
  • 其他方向同理
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp">

    <View
        android:id="@+id/divider"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignRight="@id/divider"
        android:layout_alignEnd="@id/divider"
        android:background="@android:color/darker_gray" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_alignLeft="@id/divider"
        android:layout_alignStart="@id/divider"
        android:background="@android:color/background_dark" />

</RelativeLayout>
  1. 使用支持庫中提供的百分比布局可以輕松實(shí)現(xiàn)該布局效果

Android 的支持庫中提供了百分比布局的支持,需要引入如下支持庫:

    implementation 'com.android.support:percent:26.1.0'

百分比布局提供了 PercentFrameLayout 和 PercentRelativeLayout 這兩個布局容器颓帝,使我們能夠方便地對其內(nèi)容以百分比的形式聲明其尺寸米碰、邊距,以進(jìn)行靈活的布局购城。

  • 支持以百分比聲明尺寸的屬性:
    • heightPercent
    • widthPercent
    • marginBottomPercent
    • marginEndPercent
    • marginLeftPercent
    • marginPercent
    • marginRightPercent
    • marginStartPercent
    • marginTopPercent

如下吕座,使用 PercentRelativeLayout 實(shí)現(xiàn)了左右均分的布局效果。

<?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="100dp">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/darker_gray"
        app:layout_heightPercent="100%"
        app:layout_widthPercent="50%" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@android:color/background_dark"
        app:layout_heightPercent="100%"
        app:layout_widthPercent="50%" />

</android.support.percent.PercentRelativeLayout>
  1. 更為靈活的 ConstraintLayout 可以輕松實(shí)現(xiàn)各種靈活布局

ConstraintLayout 與 RelativeLayout 非常相似瘪板,都是描述容器中各個成員的相對位置及相互約束進(jìn)行靈活布局的方式吴趴。ConstraintLayout 靈活、強(qiáng)大侮攀,而且其布局也能夠減少視圖層級锣枝、提升繪制效率厢拭,但是對其語法和使用方式還不甚熟悉,所以并未在項(xiàng)目實(shí)踐中大量使用撇叁。值得一提的是供鸠,Android Studio 中為 ConstraintLayout 提供了功能強(qiáng)大的布局編輯器,即便不熟悉其語法陨闹,也能夠輕松使用其實(shí)現(xiàn)相應(yīng)布局楞捂。

如下,使用 ConstraintLayout 實(shí)現(xiàn)了左右均分的布局正林,實(shí)現(xiàn)的過程也并未手寫代碼,只是使用布局編輯器拖拽了相應(yīng)的控件觅廓,并且使用其“推斷”功能生成布局,最后進(jìn)行了微調(diào)而已帖蔓。

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_left"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/darker_gray"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/tv_right"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_right"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="@android:color/background_dark"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/tv_left"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>
  1. 使用 FlexboxLayout 進(jìn)行布局

Flexbox 布局(彈性盒布局)本是使用 css、用于網(wǎng)頁布局的一種布局規(guī)范瞳脓,但是移動端的開發(fā)者也有了掌握的必要塑娇。一方面劫侧,這種布局方式確實(shí)方便靈活,另一方面烧栋,如今使用 RN写妥、weex 等跨平臺的開發(fā)框架時(shí),少不了使用 flexbox 布局审姓。

瀏覽一遍該網(wǎng)站珍特,即可對 css 的幾種基本布局方式有個大致的了解魔吐;過一遍阮一峰前輩的flex布局教程,便能夠使用 flexbox 進(jìn)行常用的布局酬姆,這個教程語法轴踱、實(shí)例豐富症脂,可以當(dāng)做學(xué)習(xí)材料,也可以當(dāng)做開發(fā)過程中的參考壶唤。

Google 官方提供了 flexbox-layout棕所,這一個庫來為 Android 提供了 彈性盒布局的功能,其基本的語法與 css 的語法保持一致琳省。RN 與 weex 能夠支持 flexbox 布局,實(shí)質(zhì)上也是在 Android 和 iOS 兩端的 SDK 中分別實(shí)現(xiàn)了一套原生的击费、符合 flexbox 語法規(guī)范的自定義布局而已桦他。

如下,我們使用 flexbox 布局快压,在 Android 上實(shí)現(xiàn)了一個簡單的左右均分布局。

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.flexbox.FlexboxLayout 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="100dp"
    app:flexDirection="row"
    app:flexWrap="nowrap">

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        app:layout_flexGrow="1" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:background="@android:color/background_dark"
        app:layout_flexGrow="1" />

</com.google.android.flexbox.FlexboxLayout>

文中代碼

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市脉幢,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌绸罗,老刑警劉巖豆瘫,帶你破解...
    沈念sama閱讀 222,183評論 6 516
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件菊值,死亡現(xiàn)場離奇詭異,居然都是意外死亡腻窒,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,850評論 3 399
  • 文/潘曉璐 我一進(jìn)店門瓦哎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人割岛,你說我怎么就攤上這事犯助。” “怎么了剂买?”我有些...
    開封第一講書人閱讀 168,766評論 0 361
  • 文/不壞的土叔 我叫張陵瞬哼,是天一觀的道長。 經(jīng)常有香客問我坐慰,道長,這世上最難降的妖魔是什么两残? 我笑而不...
    開封第一講書人閱讀 59,854評論 1 299
  • 正文 為了忘掉前任把跨,我火速辦了婚禮,結(jié)果婚禮上着逐,老公的妹妹穿的比我還像新娘耸别。我一直安慰自己健芭,他們只是感情好秀姐,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,871評論 6 398
  • 文/花漫 我一把揭開白布省有。 她就那樣靜靜地躺著,像睡著了一般蠢沿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上恤磷,一...
    開封第一講書人閱讀 52,457評論 1 311
  • 那天,我揣著相機(jī)與錄音魔策,去河邊找鬼锌妻。 笑死代乃,一個胖子當(dāng)著我的面吹牛仿粹,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播堕仔,決...
    沈念sama閱讀 40,999評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼晌区,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了恼五?” 一聲冷哼從身側(cè)響起哭懈,我...
    開封第一講書人閱讀 39,914評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎睬罗,沒想到半個月后旭斥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體容达,經(jīng)...
    沈念sama閱讀 46,465評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡花盐,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,543評論 3 342
  • 正文 我和宋清朗相戀三年菇爪,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,675評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡近速,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出削葱,到底是詐尸還是另有隱情,我是刑警寧澤析砸,帶...
    沈念sama閱讀 36,354評論 5 351
  • 正文 年R本政府宣布首繁,位于F島的核電站,受9級特大地震影響弦疮,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜咏尝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,029評論 3 335
  • 文/蒙蒙 一啸罢、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧扰才,春花似錦、人聲如沸累驮。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,514評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽午绳。三九已至,卻和暖如春蜡坊,著一層夾襖步出監(jiān)牢的瞬間赎败,已是汗流浹背秕衙。 一陣腳步聲響...
    開封第一講書人閱讀 33,616評論 1 274
  • 我被黑心中介騙來泰國打工据忘, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人勇吊。 一個月前我還...
    沈念sama閱讀 49,091評論 3 378
  • 正文 我出身青樓,卻偏偏與公主長得像汉规,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子针史,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,685評論 2 360

推薦閱讀更多精彩內(nèi)容