Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

動手做新浪微博客戶端(6.1):閱讀微博頁面的 UI 拆解

編輯:Android 開發實例

上一篇把微博列表做完了,這一篇接著做「點進去看單篇微博」的閱讀頁。焦點放在版面:一個看似很長的 layout,其實只是把畫面切成四塊再各自排版而已。

一、版面切成四段

整個頁面自上而下分成:

  1. 頂部工具列(返回、首頁、標題)
  2. 作者頭像與暱稱
  3. 微博正文(可捲動)
  4. 底部功能按鈕區(關注、評論、重新整理、收藏)

新增 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>

三、按鈕反白:selector 才是主角

版面本身沒什麼難度,真正讓介面「有反應」的是按下時換圖的效果。以關注鈕為例,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 把正文與配圖包起來,超出時自動出現捲動條,不必自己處理滾動邏輯。要注意的是 ScrollView 只能直接容納一個子檢視,所以裡面還得墊一層 LinearLayout。

下一篇再來把這些按鈕的實際行為(重新整理、評論、收藏)接上。

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