1.<include/>
<include/>標簽作用重用布局
- include標簽可以使用單獨的layout屬性
- include標簽指定了id,同時layout也定義了id,則layout的id會被覆蓋
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:background="@color/app_bg"
android:gravity="center_horizontal">
<include layout="@layout/titlebar"/>
<TextView android:layout_width=”match_parent”
android:layout_height="wrap_content"
android:text="@string/hello"
android:padding="10dp" />
...
</LinearLayout>
2.<merge/>
減少視圖層級的復用,<merge/>標簽在UI結構化優化起著非常重要的作用,可以減少多余的層級,優化UI。
使用:
- 用于替換FrameLayout
- 當一個布局包含另一個布局時,如一個垂直的線性布局,引入了一個垂直的include,使用merge可以消除include布局中的線性布局。
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/delete"/>
</merge>
3.<viewstub>
需要時加載,優化初始化UI性能,如不常用的進度條、錯誤消息等使用viewstub,減少內存使用量,加快布局渲染。它是一個不可見,大小為0的view。
<ViewStub
android:id="@+id/stub_import"
android:inflatedId="@+id/panel_import"
android:layout="@layout/progress_overlay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" />
使用時加載:
((ViewStub) findViewById(R.id.stub_import)).setVisibility(View.VISIBLE);
// or
View importPanel = ((ViewStub) findViewById(R.id.stub_import)).inflate();
當調用inflate函數的時候,viewstub被引用的資源替代,并返回引用的view,這樣程序可以直接得到view的引用而不用再次調用findViewById來查找
注:viewstub目前不支持merge標簽。