這兩個實用的布局技巧,作為 Android 開發的你,不能錯過
Android 布局容器、常用控件和屬性,相信每個開發者都能倒背如流,開發排版 layout 時也能適當取舍。但是,本文中介紹的這兩個常見的設計場景,其特殊的實現技巧可能你真的不曾用過。
RelativeLayout 水平居中等分
設計場景:
看到這樣的效果,可能你會不假思索地選擇 LinearLayout 容器,同時分配 children 的 weight 屬性。不錯,這樣實現確實很簡單。但是,通常界面上還有其他元素,父容器一般使用的是 RelativeLayout ,如果再選擇使用一層 LinearLayout 包裹這兩個 Button 的話,無疑會額外增加視圖層次(View Hierarchy),增加性能渲染壓力。其實,大可不必這樣做, RelativeLayout 也能讓兩個 children 水平居中等分寬度。
實現方式如下:
<?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 內容適配技巧
設計場景:
簡單說明一下,這種界面,或者說類似的界面很常見,中間內容長度不固定,或者說相同內容在不同設備上屏幕占比不一致,導致底部操作按鈕位置也不盡相同。比如,這個界面的產品重點是希望用戶能夠讀完協議內容,然后再做出下一步操作。也就是說,你不能做成這個樣子:
可以對比著前面那張圖看一下,產品希望的實現效果是:當屏幕空間足夠大(或者說中間內容較少)時,操作按鈕顯示在屏幕底部;當屏幕空間不足(或者說中間內容較多)時,以內容顯示為主,操作按鈕位于屏幕之外,向上滾動方可進入屏幕可見范圍,然后進行下一步操作。
理解需求之后,我們再來看一下實現方式:
<?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="用戶協議"
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>
注意兩個地方,第一個是設置 ScrollView 的 android:fillViewport="true" 屬性,保證內容占據整個屏幕空間;第二個地方是,中間部分,也就是例子中的 TextView 巧妙運用 android:layout_weight 屬性,實現高度上的自適應,保證后面的操作按鈕部分能夠按照我們期望的要求正確展示。
好好領會一下,相信我們一定能夠有所收獲。并且你會發現,實際開發過程中能夠運用上述兩個布局技巧的地方會有很多。當然,讀完此文,如果你還用過其他比較特別的技巧的話,不妨留言交流一下。
來自:http://yifeng.studio/2017/03/08/android-several-layout-skills/