Android 布局容器纳猪、常用控件和屬性,相信每個開發(fā)者都能倒背如流桃笙,開發(fā)排版 layout 時也能適當(dāng)取舍氏堤。但是,本文中介紹的這兩個常見的設(shè)計場景怎栽,其特殊的實(shí)現(xiàn)技巧可能你真的不曾用過丽猬。
RelativeLayout 水平居中等分
設(shè)計場景:
看到這樣的效果,可能你會不假思索地選擇 LinearLayout
容器熏瞄,同時分配 children 的 weight 屬性脚祟。不錯,這樣實(shí)現(xiàn)確實(shí)很簡單强饮。但是由桌,通常界面上還有其他元素,父容器一般使用的是 RelativeLayout
邮丰,如果再選擇使用一層 LinearLayout
包裹這兩個 Button 的話,無疑會額外增加視圖層次(View Hierarchy)剪廉,加大性能渲染壓力。其實(shí)捌斧,大可不必這樣做,RelativeLayout
也能讓兩個 children 水平居中等分寬度泉沾。
實(shí)現(xiàn)方式如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/hello"
android:layout_toRightOf="@+id/view"
android:text="First" />
<View
android:id="@+id/view"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/view"
android:text="Second" />
</RelativeLayout>
ScrollView 內(nèi)容適配技巧
設(shè)計場景:
簡單說明一下捞蚂,這種界面跷究,或者說類似的界面很常見,中間內(nèi)容長度不固定俊马,或者說相同內(nèi)容在不同設(shè)備上屏幕占比不一致丁存,導(dǎo)致底部操作按鈕位置也不盡相同。比如柴我,這個界面的產(chǎn)品重點(diǎn)是希望用戶能夠讀完協(xié)議內(nèi)容柱嫌,然后再做出下一步操作屯换。也就是說,你不能做成這個樣子:
可以對比著前面那張圖看一下嘉抓,產(chǎn)品希望的實(shí)現(xiàn)效果是:當(dāng)屏幕空間足夠大(或者說中間內(nèi)容較少)時晕窑,操作按鈕顯示在屏幕底部;當(dāng)屏幕空間不足(或者說中間內(nèi)容較多)時杨赤,以內(nèi)容顯示為主,操作按鈕位于屏幕之外植捎,向上滾動方可進(jìn)入屏幕可見范圍,然后進(jìn)行下一步操作蚓峦。
理解需求之后济锄,我們再來看一下實(shí)現(xiàn)方式:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="用戶協(xié)議"
android:textAppearance="?android:attr/textAppearanceMedium" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:background="@color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="16dp"
android:text="@string/content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/bottom_bar"
android:gravity="center_vertical">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="同意" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="拒絕" />
</LinearLayout>
</LinearLayout>
</ScrollView>
注意兩個地方荐绝,第一個是設(shè)置 ScrollView 的 android:fillViewport="true"
屬性,保證內(nèi)容占據(jù)整個屏幕空間角虫;第二個地方是委造,中間部分戳鹅,也就是例子中的 TextView
巧妙運(yùn)用 android:layout_weight
屬性昏兆,實(shí)現(xiàn)高度上的自適應(yīng),保證后面的操作按鈕部分能夠按照我們期望的要求正確展示隶债。
好好領(lǐng)會一下跑筝,相信我們一定能夠有所收獲。并且你會發(fā)現(xiàn)赞警,實(shí)際開發(fā)過程中能夠運(yùn)用上述兩個布局技巧的地方會有很多虏两。當(dāng)然,讀完此文定罢,如果你還用過其他比較特別的技巧的話,不妨留言交流一下琼蚯。