在 activity_main.xml 中 <include layout="@layout/title"/> 後,標題欄佔滿整個螢幕。原因是 title.xml 根元素高度設為 match_parent,應改為 wrap_content。
<LinearLayout xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="match_parent"> <-- 錯誤
...
</LinearLayout>
<LinearLayout xmlns:android="..."
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bg"
android:orientation="horizontal">
<Button android:id="@+id/title_back" ... />
<TextView android:id="@+id/title_text" ... />
<Button android:id="@+id/title_edit" ... />
</LinearLayout>
<LinearLayout ... android:orientation="vertical">
<include layout="@layout/title" />
<!-- 其他內容 -->
</LinearLayout>
include 會將被引入佈局的根元素屬性合併到父佈局同位置,故根元素尺寸決定顯示大小。findViewById(R.id.title_root) 操作 visibility。