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

仿 QQ 空間主頁實作:PopubWindow 打造動態選單與發佈按鈕

編輯:Android 開發實例

這次來模仿一下手機 QQ 空間的主頁。啟動畫面與引導頁就不做了,那部分參考微信的寫法即可,很簡單。重點放在主頁的版面結構與兩個彈出選單的互動上。

版面結構拆解

整個主頁可以拆成三塊:最上方的標題列(點擊會展開分類選單)、中間的內容區、以及最下方的圓角發佈按鈕與導航列。

外層容器

最外層只用一個 LinearLayout 把 main 這個版面 include 進來,好處是後續要替換或複用時比較彈性。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

    <include
        android:id="@+id/top_layout"
        layout="@layout/main" />
</LinearLayout>

標題列與底部導航列(main.xml)

標題列用 RelativeLayout,標題文字兩側各放一張上下箭頭圖示,透過切換 visibility 做出展開/收起的視覺暗示。底部則是 FrameLayout 疊 LinearLayout 的組合:LinearLayout 平均分配四個分頁的圖示與文字,FrameLayout 則把中央那顆突出的圓形發佈按鈕疊在最上層。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 最上方「我的空間動態」-->
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="40dip"
        android:layout_alignParentTop="true"
        android:background="@drawable/navbar_bg" >

        <TextView
            android:id="@+id/titltext"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="我的空間動態"
            android:textSize="20dp"
            android:layout_centerInParent="true" />

        <ImageView
            android:id="@+id/bytextimage1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/navbar_drop_down"
            android:layout_toRightOf="@id/titltext" />

        <ImageView
            android:id="@+id/bytextimage2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/navbar_drop_up"
            android:visibility="invisible"
            android:layout_toRightOf="@id/titltext" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:src="@drawable/icon_refresh_none" />
    </RelativeLayout>

    <!-- 底部導航欄 -->
    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="65.32999dip"
        android:layout_alignParentBottom="true"
        android:background="@drawable/toolbar_bg" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="48.0dip"
            android:layout_gravity="bottom"
            android:baselineAligned="false"
            android:gravity="center_horizontal"
            android:orientation="horizontal" >

            <!-- 好友動態 -->
            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="48.0dip"
                android:layout_weight="1.0" >
                <ImageView
                    android:layout_width="28.0dip"
                    android:layout_height="28.0dip"
                    android:layout_gravity="top|center"
                    android:layout_marginTop="1.0dip"
                    android:src="@drawable/tab_timefeed_opacity" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|center"
                    android:layout_marginBottom="6.0dip"
                    android:text="好友動態"
                    android:textColor="#ffffffff"
                    android:textSize="10.0dip" />
            </FrameLayout>

            <!-- 與我相關(附未讀數量紅點)-->
            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="48.0dip"
                android:layout_weight="1.0" >
                <ImageView
                    android:layout_width="27.0dip"
                    android:layout_height="27.0dip"
                    android:layout_gravity="top|center"
                    android:src="@drawable/tab_feedback_opacity" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|center"
                    android:layout_marginBottom="6.0dip"
                    android:text="與我相關"
                    android:textColor="#ffffffff"
                    android:textSize="10.0dip" />
                <TextView
                    android:layout_width="17.0dip"
                    android:layout_height="17.0dip"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginBottom="15.0dip"
                    android:layout_marginLeft="10.0dip"
                    android:background="@drawable/myhome_visitor_number"
                    android:gravity="center_vertical"
                    android:text="n"
                    android:textColor="#ffffffff"
                    android:textSize="13.0dip" />
            </FrameLayout>

            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1.0" />

            <!-- 主頁 -->
            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="48.0dip"
                android:layout_weight="1.0" >
                <ImageView
                    android:layout_width="27.0dip"
                    android:layout_height="27.0dip"
                    android:layout_gravity="top|center"
                    android:src="@drawable/tab_myzone_opacity" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|center"
                    android:layout_marginBottom="6.0dip"
                    android:text="主頁"
                    android:textColor="#ffffffff"
                    android:textSize="10.0dip" />
            </FrameLayout>

            <!-- 應用 -->
            <FrameLayout
                android:layout_width="fill_parent"
                android:layout_height="48.0dip"
                android:layout_weight="1.0" >
                <ImageView
                    android:layout_width="27.0dip"
                    android:layout_height="27.0dip"
                    android:layout_gravity="top|center"
                    android:src="@drawable/tab_applist_opacity" />
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|center"
                    android:layout_marginBottom="6.0dip"
                    android:text="應用"
                    android:textColor="#ffffffff"
                    android:textSize="10.0dip" />
            </FrameLayout>
        </LinearLayout>

        <!-- 中央突出的圓形發佈按鈕 -->
        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" >
            <ImageView
                android:layout_width="66.666595dip"
                android:layout_height="65.33328dip"
                android:layout_gravity="center"
                android:src="@drawable/toolbar_write_bg" />
        </FrameLayout>

        <FrameLayout
            android:id="@+id/btn_ck"
            android:layout_width="65.33328dip"
            android:layout_height="65.33328dip"
            android:layout_gravity="center" >
            <ImageView
                android:id="@+id/image1"
                android:layout_width="22.666595dip"
                android:layout_height="22.666595dip"
                android:layout_gravity="center"
                android:layout_marginTop="2.0dip"
                android:src="@drawable/toolbar_plus" />
        </FrameLayout>
    </FrameLayout>
</RelativeLayout>

Activity:用 PopupWindow 實作兩個彈出層

點標題展開分類清單、點中央圓鈕展開發佈選單,兩者都用 PopupWindow。這裡有幾個設定是刻意的:

setFocusable(true) 讓彈出層取得焦點;setOutsideTouchable(true) 讓點擊外部可以關閉;setBackgroundDrawable(new BitmapDrawable()) 這行看似多餘,實際上是必要的——有了背景物件,按返回鍵才會先關閉 PopupWindow 而不是直接結束 Activity,而且不會影響視覺。

package com.example.imitateqqzone;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class ImitateQQZone extends Activity {

    private PopupWindow popupWindow1, popupWindow2;
    private ListView lv_group;
    private View view, top_layout;
    private TextView tvtitle;
    private List<String> groups;
    private ImageView byTextImageView1, byTextImageView2, centerimage;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_imitate_qqzone);

        top_layout = this.findViewById(R.id.top_layout);
        tvtitle = (TextView) top_layout.findViewById(R.id.titltext);
        byTextImageView1 = (ImageView) top_layout.findViewById(R.id.bytextimage1);
        byTextImageView2 = (ImageView) top_layout.findViewById(R.id.bytextimage2);
        centerimage = (ImageView) findViewById(R.id.image1);

        // setVisibility 的三個值:
        // 0 = VISIBLE    可見
        // 4 = INVISIBLE  不可見但保留佔位空間
        // 8 = GONE       不可見且不佔位
        tvtitle.setOnClickListener(new OnClickListener() {
            boolean isImage = false;

            public void onClick(View v) {
                showWindow1(v);
                if (!isImage) {
                    isImage = true;
                    byTextImageView1.setVisibility(4);   // 換成向上箭頭
                    byTextImageView2.setVisibility(0);
                } else {
                    isImage = false;
                    byTextImageView1.setVisibility(0);   // 換回向下箭頭
                    byTextImageView2.setVisibility(4);
                }
            }
        });

        centerimage.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                showWindow2(v);
            }
        });
    }

    /** 中央圓鈕的發佈選單 */
    protected void showWindow2(View v) {
        if (popupWindow2 == null) {
            LayoutInflater inflater = (LayoutInflater)
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.pubmenu, null);
            popupWindow2 = new PopupWindow(view, 400, 400);
        }
        popupWindow2.setFocusable(true);
        popupWindow2.setOutsideTouchable(true);
        popupWindow2.setBackgroundDrawable(new BitmapDrawable());
        popupWindow2.showAsDropDown(v);
    }

    /** 標題列的分類清單 */
    private void showWindow1(View parent) {
        if (popupWindow1 == null) {
            LayoutInflater inflater = (LayoutInflater)
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.group_list, null);
            lv_group = (ListView) view.findViewById(R.id.lvGroup);

            // 載入資料
            groups = new ArrayList<String>();
            groups.add("全部動態");
            groups.add("好友動態");
            groups.add("特別關心");
            groups.add("認證空間");

            lv_group.setAdapter(new GroupAdapter(this, groups));
            popupWindow1 = new PopupWindow(view, 200, 350);
        }

        popupWindow1.setFocusable(true);
        popupWindow1.setOutsideTouchable(true);
        popupWindow1.setBackgroundDrawable(new BitmapDrawable());
        popupWindow1.showAsDropDown(parent);

        lv_group.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view,
                                    int position, long id) {
                Toast.makeText(ImitateQQZone.this, groups.get(position), 1000).show();
                if (popupWindow1 != null) {
                    popupWindow1.dismiss();
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_imitate_qqzone, menu);
        return true;
    }
}

幾點補充

其餘的版面檔(group_list.xml、pubmenu.xml)與 GroupAdapter 這裡沒有貼出,需要的話可以再補。

這只是個 Demo,基於時間因素還有許多細節沒實作,例如分頁切換時的內容替換、選單選取後的資料過濾、以及中央按鈕的旋轉動畫等,都可以在這個基礎上繼續完善。

另外提醒一個實務上的建議:底部導航若是主要的分頁切換,用 TabLayout 搭配 ViewPager2 會比手寫 FrameLayout 更好維護,選中態的樣式與切換動畫都有現成 API 可用,也省下大量版面檔的重複定義。

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