編輯:關於 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>
加上這兩行,執行後就能看到片段裡的內容出現在對應位置。
這是最多人卡住的地方:同一個片段被 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,能讓版面結構清楚、維護成本大幅下降,是中小型專案很值得一開始就建立的好習慣。
Android Toolbar 開發實戰全解析
一、走進 Toolbar 從 Android 5.0 起,Google 推出了 Toolbar 這個充滿 Material Design 風格的導航控件,並大力建議開發者
Android 自訂控件分享:讓數字動起來的 DancingNumberView
簡介 DancingNumberView 是一個專門用來「跳動顯示數字」的控件,直接繼承自 TextView。這類效果常用在金額這類使用者比較敏感的數字上,讓 UI 互動
高德地圖在部分手機報 Native method not found?缺少 x86 so 庫惹的禍
踩到一個只在特定機型出現的問題:使用高德地圖時拋出下面的錯誤,其他機型都正常,只有聯想的部分手機會復現。 java.lang.UnsatisfiedLinkError:
Android開發實戰:Fragment與Activity間的通信範例
首先,如果你想在Android 3.0及以下版本使用Fragment,你必須引用android-support-v4.jar這個包。然後,你寫的Activity不能再繼承