QQ空間實現(二)—— 分享功能 / 彈出PopupWindow

来源:http://www.cnblogs.com/xqxacm/archive/2016/03/02/5234849.html
-Advertisement-
Play Games

這是一張QQ空間說說詳情的截圖。 分析: 1、點擊右上角三個點的圖標,在界面底部彈出一個區域,這個區域有一些按鈕提供給我們操作 2、當該區域出現的時候,詳情界面便灰了,也說成透明度變化了 3、當任意選了一個按鈕或者點擊了該區域以外的部分,該區域消失,灰色界面變回亮白色,並執行點擊的按鈕對應的操作 顯


這是一張QQ空間說說詳情的截圖。

分析:

1、點擊右上角三個點的圖標,在界面底部彈出一個區域,這個區域有一些按鈕提供給我們操作
2、當該區域出現的時候,詳情界面便灰了,也說成透明度變化了
3、當任意選了一個按鈕或者點擊了該區域以外的部分,該區域消失,灰色界面變回亮白色,並執行點擊的按鈕對應的操作

顯然,這個功能我們需要用PopupWindow實現更好~

--------------------------------------------------------------------------------------

下麵通過一個Demo來實現這個需求~~

效果圖:

首先還是佈局文件:

1、主界面:

我們只需要在界面的右上角放一個按鈕來彈出PopupWindow ,註意 父容器需要有一個id,因為我們需要它來給PopupWindow設置彈出的位置

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:id="@+id/mainlayout"
    >

    <ImageButton
        android:id="@+id/btn_more"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/btn_more"
        android:background="#0000"
        android:layout_alignParentRight="true"
        android:layout_margin="5dp"
        />

</RelativeLayout>

2、PopupWindow界面

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:orientation="vertical"
    >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical|center"
        android:background="@drawable/shape_rectangle_white"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/shareto"
            android:layout_width="match_parent"
            android:layout_height="35dp"
            android:gravity="center"
            android:layout_gravity="center_horizontal"
            android:text="分享到"
            android:textSize="15sp"
             />

            <LinearLayout
                android:id="@+id/fp_linear_share"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:layout_marginBottom="5dp">


                <LinearLayout
                    android:id="@+id/fp_linear_sharetoWeixin"
                    android:layout_weight="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:gravity="center_horizontal"
                    >
                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:background="@mipmap/ic_launcher"
                        android:layout_gravity="bottom|center_horizontal"/>
                    <TextView
                        android:layout_marginTop="4dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="微信聯繫人"
                        android:gravity="center"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/fp_linear_sharetoquan"
                    android:layout_weight="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:gravity="center_horizontal"
                    >
                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:background="@mipmap/ic_launcher"
                        android:layout_gravity="bottom|center_horizontal"/>
                    <TextView
                        android:layout_marginTop="4dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="微信朋友圈"
                        android:gravity="center"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/fp_linear_sharetoQzone"
                    android:layout_weight="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:gravity="center_horizontal"
                    >
                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:background="@mipmap/ic_launcher"
                        android:layout_gravity="bottom|center_horizontal"/>
                    <TextView
                        android:layout_marginTop="4dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="QQ空間"
                        android:gravity="center"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/fp_linear_sharetoQQ"
                    android:layout_weight="1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:gravity="center_horizontal"
                    >
                    <ImageView
                        android:layout_width="55dp"
                        android:layout_height="55dp"
                        android:background="@mipmap/ic_launcher"
                        android:layout_gravity="bottom|center_horizontal"/>
                    <TextView
                        android:layout_marginTop="4dp"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="QQ好友"
                        android:gravity="center"
                        />
                </LinearLayout>

            </LinearLayout>

            <View
                android:layout_marginTop="20dp"
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="#e8e8e8" />

        <LinearLayout
            android:id="@+id/fp_other"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginTop="25dp"
            android:layout_marginBottom="5dp">
            <LinearLayout
                android:id="@+id/fp_report"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                >
                <ImageView
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:background="@mipmap/ic_launcher"
                    android:layout_gravity="bottom|center_horizontal"/>
                <TextView
                    android:layout_marginTop="4dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="舉報"
                    android:gravity="center"
                    />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/fp_hide_all"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                >
                <ImageView
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:background="@mipmap/ic_launcher"
                    android:layout_gravity="bottom|center_horizontal"/>
                <TextView
                    android:layout_marginTop="4dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="屏蔽此人"
                    android:gravity="center"
                    />
            </LinearLayout>
            <LinearLayout
                android:id="@+id/fp_hide_pic"
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                >
                <ImageView
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:background="@mipmap/ic_launcher"
                    android:layout_gravity="bottom|center_horizontal"/>
                <TextView
                    android:layout_marginTop="4dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="屏蔽此照片"
                    android:gravity="center"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_weight="1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:gravity="center_horizontal"
                >
                <ImageView
                    android:layout_width="55dp"
                    android:layout_height="55dp"
                    android:layout_gravity="bottom|center_horizontal"/>
                <TextView
                    android:layout_marginTop="4dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    />
            </LinearLayout>

        </LinearLayout>
            <View

                android:layout_width="match_parent"
                android:layout_height="0.5dp"
                android:background="#e8e8e8" />
            <TextView
                android:id="@+id/fp_cancel"
                android:layout_width="fill_parent"
                android:layout_height="50dp"
                android:gravity="center"        
                android:text="取消"
                android:textSize="17sp" />
    </LinearLayout>
</RelativeLayout>
popupwindow

  

--------------------------------------------------------------------------------------

 java代碼部分:

1、首先我們要自定義一個繼承PopupWindow的類(根據項目需求決定定義的內容)/**

 * 自定義PopupWindow , 實現仿QQ空間分享效果
 */
public class SelectPopupWindow extends PopupWindow {

    //一個LinearLayout 表示一個可選操作
    private LinearLayout fp_hide_all,fp_hide_pic,fp_report,fp_linear_sharetoWeixin,fp_linear_sharetoquan,fp_linear_sharetoQzone,fp_linear_sharetoQQ;
    //popupWindow 取消文本按鈕
    private TextView fp_cancel;
private View mMenuView; public SelectPopupWindow(Activity context, OnClickListener itemsOnClick) { super(context); LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.feed_popuwindow, null);
fp_hide_pic
= (LinearLayout) mMenuView.findViewById(R.id.fp_hide_pic); fp_hide_all = (LinearLayout) mMenuView.findViewById(R.id.fp_hide_all); fp_report = (LinearLayout) mMenuView.findViewById(R.id.fp_report); fp_linear_sharetoWeixin = (LinearLayout) mMenuView.findViewById(R.id.fp_linear_sharetoWeixin); fp_linear_sharetoquan = (LinearLayout) mMenuView.findViewById(R.id.fp_linear_sharetoquan); fp_linear_sharetoQzone = (LinearLayout) mMenuView.findViewById(R.id.fp_linear_sharetoQzone); fp_linear_sharetoQQ = (LinearLayout) mMenuView.findViewById(R.id.fp_linear_sharetoQQ); fp_cancel = (TextView) mMenuView.findViewById(R.id.fp_cancel); //點擊取消按鈕,關閉popupWindow fp_cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); fp_hide_pic.setOnClickListener(itemsOnClick); fp_hide_all.setOnClickListener(itemsOnClick); fp_report.setOnClickListener(itemsOnClick); fp_linear_sharetoWeixin.setOnClickListener(itemsOnClick); fp_linear_sharetoquan.setOnClickListener(itemsOnClick); fp_linear_sharetoQzone.setOnClickListener(itemsOnClick); fp_linear_sharetoQQ.setOnClickListener(itemsOnClick); this.setContentView(mMenuView); this.setWidth(LayoutParams.MATCH_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); ColorDrawable dw = new ColorDrawable(0x000000); this.setBackgroundDrawable(dw); this.setFocusable(true);
    
//點擊popupWindow之外的部分 關閉popupWindow mMenuView.setOnTouchListener(
new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.fp_linear_share).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP){ if(y<height){ dismiss(); } } return true; } }); } // 可自主添加其他功能需求方法 }

 

最後看MainActivity.java

public class MainActivity extends Activity implements View.OnClickListener {
// 自定義PopupWindow
private SelectPopupWindow feedSelectPopupWindow;
// 界面父容器
private RelativeLayout relativeLayout;
// 打開popupWindow的按鈕
private ImageButton btn_more;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout) findViewById(R.id.mainlayout);
btn_more = (ImageButton) findViewById(R.id.btn_more);
btn_more.setOnClickListener(this);
}

// popupWindow 點擊事件監聽
private View.OnClickListener selectItemsOnClick = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
//根據popupWindow 佈局文件中的id 來執行相應的點擊事件
case R.id.fp_linear_sharetoWeixin:
Toast.makeText(MainActivity.this,"點擊了微信分享",Toast.LENGTH_SHORT).show();
break;
// ....
}
//每次點擊popupWindow中的任意按鈕,記得關閉此popupWindow,
feedSelectPopupWindow.dismiss();
}
};

/**
* 設置添加屏幕的背景透明度
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
//點擊分享按鈕,彈出PopupWindow
case R.id.btn_more:
feedSelectPopupWindow = new SelectPopupWindow(this, selectItemsOnClick);
// 設置popupWindow顯示的位置
// 此時設在界面底部並且水平居中
feedSelectPopupWindow.showAtLocation(relativeLayout,
Gravity.BOTTOM| Gravity.CENTER_HORIZONTAL, 0, 0);
// 當popupWindow 出現的時候 屏幕的透明度 ,設為0.5 即半透明 灰色效果
backgroundAlpha(0.5f);
// 設置popupWindow取消的點擊事件,即popupWindow消失後,屏幕的透明度,全透明,就回覆原狀態
feedSelectPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
break;
}
}
}

 

註意點:

如果你在你自己的項目中使用了彈出PopupWindow,報錯如下:

 Unable to add window -- token null is not valid; is your activity running?

一般是錯誤在 .showAtLocation()方法上,那麼要註意PopupWindow和Dialog一樣是需要依賴於Activity存在的

所以不要在onCreate()方法中使用 .showAtLocation()方法 ,因為這個時候Activity還沒有Create()完成

 

--------------------------------------------------------------------------------------

相關知識:

QQ空間實現(一)—— 展示說說中的評論內容並有相應點擊事件

 

博主現在從事社交類社區類APP開發,有同領域的朋友歡迎關註交流~~~


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言:你用過Eclipse快捷鍵 Alt + Shift + J 麽?你看過源碼麽?如果看過,你註意過源碼上面的註釋麽?你知道為什麼看源碼註釋有些標識的參數可以直接點擊跳轉麽? 先出個題目,定義一個最簡單的Person類,三個屬性,一個name,一個age,一個性別,一個帶所有屬性參數的構造函數,你
  • 使用實例詳細闡述 日曆簽到功能 的實現方案,及 項目文件結構的設置方法
  • 前言 本篇文章精講iOS開發中使用Block時一定要註意記憶體管理問題,很容易造成迴圈引用。本篇文章的目標是幫助大家快速掌握使用block的技巧。 我相信大家都覺得使用block給開髮帶來了多大的便利,但是有很多開發者對block記憶體管理掌握得不夠好,導致經常出現迴圈引用的問題。對於新手來說,出現迴圈
  • 目錄 Android多解析度適配實踐【0】基礎適配篇(撰寫中)Android多解析度適配實踐【1】使用字體圖標,精準控制不同解析度的圖標樣式(內含兩枚神器)Android多解析度適配實踐【2】Iconify中文使用說明Android多解析度適配實踐【3】Iconify擴展,自製或導入SVG字體圖標庫
  • 序言 網路連接狀態檢測對於我們的iOS app開發來說是一個非常通用的需求。為了更好的用戶體驗,我們會在無網路時展現本地或者緩存的內容,並對用戶進行合適的提示。對絕大部分iOS開發者來說,從蘋果示例代碼改變而來的各種Reachablity框架是實現這個需求的普遍選擇,比如這個庫。但事實上,基於此方案
  • +(UIColor *)colorWithHexString:(NSString *)coloStr{ // 檢索 去下空格和換行 轉成大寫 NSString *cString = [[coloStr stringByTrimmingCharactersInSet:[NSCharacterSet w
  • 提高OC代碼質量的小心機 一、OC特性 OC 為 C 語言添加了面向對象特性,是其超集; OC 使用動態綁定的消息結構,也就是,在運行時才會檢查對象類型; 接收一條消息後,究竟應執行何種代碼,由運行期環境來決定,而非編譯器;ps:理解C語言的核心概念有助於寫好OC程式,尤其要掌握記憶體模型與指針。 二
  • 本文由CocoaChina--BYB_1132(論壇ID)翻譯 原文:Thoughts On AlamoFire--Swift’s AFNetworking Implementation HTTP協議就是現代開發的同義詞,對於有經驗的iOS開發者來說, 熟悉並儘可能使用這些流行的協議是日常工作的基礎
一周排行
    -Advertisement-
    Play Games
  • Dapr Outbox 是1.12中的功能。 本文只介紹Dapr Outbox 執行流程,Dapr Outbox基本用法請閱讀官方文檔 。本文中appID=order-processor,topic=orders 本文前提知識:熟悉Dapr狀態管理、Dapr發佈訂閱和Outbox 模式。 Outbo ...
  • 引言 在前幾章我們深度講解了單元測試和集成測試的基礎知識,這一章我們來講解一下代碼覆蓋率,代碼覆蓋率是單元測試運行的度量值,覆蓋率通常以百分比表示,用於衡量代碼被測試覆蓋的程度,幫助開發人員評估測試用例的質量和代碼的健壯性。常見的覆蓋率包括語句覆蓋率(Line Coverage)、分支覆蓋率(Bra ...
  • 前言 本文介紹瞭如何使用S7.NET庫實現對西門子PLC DB塊數據的讀寫,記錄了使用電腦模擬,模擬PLC,自至完成測試的詳細流程,並重點介紹了在這個過程中的易錯點,供參考。 用到的軟體: 1.Windows環境下鏈路層網路訪問的行業標準工具(WinPcap_4_1_3.exe)下載鏈接:http ...
  • 從依賴倒置原則(Dependency Inversion Principle, DIP)到控制反轉(Inversion of Control, IoC)再到依賴註入(Dependency Injection, DI)的演進過程,我們可以理解為一種逐步抽象和解耦的設計思想。這種思想在C#等面向對象的編 ...
  • 關於Python中的私有屬性和私有方法 Python對於類的成員沒有嚴格的訪問控制限制,這與其他面相對對象語言有區別。關於私有屬性和私有方法,有如下要點: 1、通常我們約定,兩個下劃線開頭的屬性是私有的(private)。其他為公共的(public); 2、類內部可以訪問私有屬性(方法); 3、類外 ...
  • C++ 訪問說明符 訪問說明符是 C++ 中控制類成員(屬性和方法)可訪問性的關鍵字。它們用於封裝類數據並保護其免受意外修改或濫用。 三種訪問說明符: public:允許從類外部的任何地方訪問成員。 private:僅允許在類內部訪問成員。 protected:允許在類內部及其派生類中訪問成員。 示 ...
  • 寫這個隨筆說一下C++的static_cast和dynamic_cast用在子類與父類的指針轉換時的一些事宜。首先,【static_cast,dynamic_cast】【父類指針,子類指針】,兩兩一組,共有4種組合:用 static_cast 父類轉子類、用 static_cast 子類轉父類、使用 ...
  • /******************************************************************************************************** * * * 設計雙向鏈表的介面 * * * * Copyright (c) 2023-2 ...
  • 相信接觸過spring做開發的小伙伴們一定使用過@ComponentScan註解 @ComponentScan("com.wangm.lifecycle") public class AppConfig { } @ComponentScan指定basePackage,將包下的類按照一定規則註冊成Be ...
  • 操作系統 :CentOS 7.6_x64 opensips版本: 2.4.9 python版本:2.7.5 python作為腳本語言,使用起來很方便,查了下opensips的文檔,支持使用python腳本寫邏輯代碼。今天整理下CentOS7環境下opensips2.4.9的python模塊筆記及使用 ...