佈局優化的目的是爲了製作出高效、可複用的UI。 本文主要是對博主在平時開發的過程中關於佈局經驗的一個總結,覺得還有些用處,記錄下來分享給大家。
HierarchyViewer
<include>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<strong><include layout="@layout/header"/></strong>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="StaticBroadcast"
android:id="@+id/btn_static"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<merge>
<FrameLayout>
<include layout="@layout/layout2"/>
</FrameLayout>
<FrameLayout>
<TextView />
</FrameLayout>
<FrameLayout>
<FrameLayout>
<TextView />
</FrameLayout>
</FrameLayout>
<merge>
<TextView />
</merge>
<FrameLayout>
<TextView />
</FrameLayout
<ViewStub>
新建一個error.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message" />
</RelativeLayout>
使用ViewStub
<RelativeLayout>xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
...
<ViewStub
android:id="@+id/error_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout="@layout/error" />
</RelativeLayout>
在代碼中顯示ViewStub可以使用