編輯:Android 開發實例
上一篇把微博列表做完了,這一篇接著做「點進去看單篇微博」的閱讀頁。焦點放在版面:一個看似很長的 layout,其實只是把畫面切成四塊再各自排版而已。
整個頁面自上而下分成:
新增 ViewActivity.java 作為閱讀頁,並在 res/layout 下新增 view.xml。
最外層是一個直向的 LinearLayout,往下依序放相對佈局構成的工具列、分隔線、作者資訊列,最後是佔滿剩餘空間的內容區:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- ① 頂部工具列 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="3px">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo_ss" />
<TextView
android:id="@+id/showName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="#343434"
android:text="閱讀微博"
android:textSize="16px" />
<ImageButton
android:id="@+id/returnBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@+id/homeBtn"
android:background="@drawable/bnt_return_selector" />
<ImageButton
android:id="@+id/homeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="12px"
android:background="@drawable/btn_home_selector" />
</RelativeLayout>
<!-- ② 分隔線 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/hr" />
<!-- ③ 作者資訊列 -->
<RelativeLayout
android:id="@+id/user_bg"
android:layout_width="fill_parent"
android:layout_height="78px"
android:paddingTop="8px"
android:paddingLeft="15px"
android:background="@drawable/u_bg_v">
<ImageView
android:id="@+id/user_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="@drawable/usericon" />
<TextView
android:id="@+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/user_icon"
android:layout_marginLeft="10px"
android:layout_marginTop="18px"
android:textColor="#000000" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="5px"
android:layout_marginTop="10px"
android:src="@drawable/sjjt" />
</RelativeLayout>
<!-- ④ 內容區:正文 + 載入動畫 + 底部按鈕 -->
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="17px"
android:paddingRight="17px"
android:paddingBottom="5px"
android:layout_above="@+id/menu_layout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="15px" />
<ImageView
android:id="@+id/pic"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</ScrollView>
<!-- 載入中動畫,預設隱藏 -->
<LinearLayout
android:id="@+id/loadingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone"
android:layout_centerInParent="true">
<ProgressBar
android:id="@+id/loading"
android:layout_width="31px"
android:layout_height="31px"
android:layout_gravity="center"
style="@style/progressStyle" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在載入"
android:textSize="12px"
android:textColor="#9c9c9c"
android:layout_gravity="center" />
</LinearLayout>
<!-- 底部功能按鈕區,兩列 TableLayout -->
<TableLayout
android:id="@+id/menu_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_alignParentBottom="true"
android:layout_marginBottom="5px">
<TableRow android:gravity="center">
<Button
android:id="@+id/btn_gz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#3882b8"
android:textSize="15px"
android:text=" 關注(1231)"
android:background="@drawable/lt_selector" />
<Button
android:id="@+id/btn_pl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#3882b8"
android:textSize="15px"
android:text=" 評論(31)"
android:background="@drawable/rt_selector" />
</TableRow>
<TableRow android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#3882b8"
android:textSize="15px"
android:text="重新整理"
android:background="@drawable/lb_selector" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#3882b8"
android:textSize="15px"
android:text="收藏"
android:background="@drawable/rb_selector" />
</TableRow>
</TableLayout>
</RelativeLayout>
</LinearLayout>
版面本身沒什麼難度,真正讓介面「有反應」的是按下時換圖的效果。以關注鈕為例,android:background="@drawable/lt_selector" 指向一份 selector 描述檔,放在 res/drawable-mdpi 底下:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tbtn_1" />
<item android:state_pressed="true"
android:drawable="@drawable/tbtn_h_1" />
</selector>
四個功能鈕之所以能拼成一個連貫的選單列,靠的就是左半、右半兩張背景圖(lt_ / rt_、lb_ / rb_)各自搭配一份 selector,讓相鄰按鈕在視覺上融為一體。
這個頁面唯一沒在前面系列出現過的元件是 ScrollView。微博正文長度不定,遇到長文會超出可視區域,用 ScrollView 把正文與配圖包起來,超出時自動出現捲動條,不必自己處理滾動邏輯。要注意的是 ScrollView 只能直接容納一個子檢視,所以裡面還得墊一層 LinearLayout。
下一篇再來把這些按鈕的實際行為(重新整理、評論、收藏)接上。
仿 iPhone 風格對話框:元件、jar 包與用法一次提供
效果圖如下: 這個對話框完全繼承並仿照 AlertDialog,只是換上自訂的外觀。由於 iPhone 的對話框大多沒有圖示,因此這裡沒有實作 setIcon。 附件包
Android 進階:藍牙隱藏 API 探秘
前文講解了 Android 藍牙的基本用法,本文則更深入一步,探討藍牙方面的隱藏 API。用過 Android 系統設置的人都知道,藍牙搜索之後可以建立配對或解除配對,但
Android 載入 Spinner 進度指示實作教學
在 Android 裡,當某項工作正在進行、想讓使用者知道「系統還在忙」,就可以用進度條來表達。進度條大致有兩種形式:帶有進度的水平進度列,以及持續旋轉的載入轉圈(Spi
Android Spinner 載入動畫:按鈕觸發與顯示控制
Spinner 是 Android 中常見的等待提示元件。當使用者觸發某個操作,但系統需要一點時間處理時,可以用轉圈動畫告知目前仍在進行中。 基本設定 在版面配置中加入