Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 佈局標籤 include 的用法:讓重複版面只寫一次

編輯:關於 Android 程式設計

寫 Android 介面時,常會遇到同一段佈局在多個頁面重複出現的情況,例如頂部標題列、底部操作列。每頁都複製一份不僅噁心,日後一改就是改一堆檔案。這時候就該用 <include> 標籤把它抽成獨立檔案。

先寫一個可重複使用的片段

以下是我們要重複使用的標題列,包含左右兩個可點擊的文字與下方一條指示線,存成 scollandlisttitle.xml:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#000000" > <TextView android:id="@+id/txt_left_f" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:text="文字" android:clickable="true" /> <TextView android:id="@+id/txt_right_f" android:layout_width="0dp" android:layout_height="50dp" android:layout_height="50dp" android:layout_weight="1" android:gravity="center" android:text="圖片" android:clickable="true" /> </LinearLayout> <View android:id="@+id/view_line" android:layout_width="50dp" android:layout_height="1dp" android:background="#FF0066" /> </LinearLayout> 

在頁面中引用它

接著在 scollandlistviewpager.xml 裡用兩次 <include>,分別放在 ViewPager 的上方與外層,做成「跟手」與「吸頂」兩個版本:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:id="@+id/vparent_layout"> <com.example.listsuspension.MyScrollView android:id="@+id/vscrollView" android:layout_width="match_parent" android:layout_height="fill_parent" android:fillViewport="true"> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/viamge" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/pic" android:scaleType="center" /> <include android:id="@+id/vtitle" layout="@layout/scollandlisttitle" /> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="fill_parent" /> </LinearLayout> <include android:id="@+id/vtop_title" layout="@layout/scollandlisttitle" /> </FrameLayout> </com.example.listsuspension.MyScrollView> </LinearLayout> 

加上這兩行,執行後就能看到片段裡的內容出現在對應位置。

id 重複怎麼辦?

這是最多人卡住的地方:同一個片段被 include 兩次,裡面的 txt_left_f、txt_right_f 自然就出現兩份。解法很簡單——先用 findViewById 取得 <include> 節點本身(靠我們給它的 android:id),再從這個節點往下找子控制項,這樣就各自獨立、不會互相干擾。

 titleLayout = (LinearLayout)findViewById(R.id.vtitle); toptitleLayout = (LinearLayout)findViewById(R.id.vtop_title); //viewPager = (ViewPager)findViewById(R.id.viewpager); //點擊按鈕事件 txt_left_fc = (TextView)toptitleLayout.findViewById(R.id.txt_left_f); txt_left_fc.setOnClickListener(this); txt_right_fc = (TextView)toptitleLayout.findViewById(R.id.txt_right_f); txt_right_fc.setOnClickListener(this); 

還可以搭配的寫法

除了 layout,<include> 也支援覆寫 android:layout_width、android:layout_height 等少數屬性,讓同一個片段在不同場合呈現不同尺寸。不過要注意:想覆寫的屬性必須在被 include 的根節點上原本就有宣告,否則不生效。

善用 include,能讓版面結構清楚、維護成本大幅下降,是中小型專案很值得一開始就建立的好習慣。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved