編輯:關於 Android 開發

這是一张QQ空間說說詳情的截圖,我們來拆解一下它的交互:
1、點擊右上角「三個點」的圖標,界面底部會彈出一個區域,裡面有一排按鈕供我們操作; 2、當該區域出現時,詳情界面會整體變灰,也就是透明度發生了變化; 3、當選擇了某個按鈕,或點擊該區域以外的部分時,該區域消失,灰色界面恢復亮白,並執行所選按鈕對應的操作。
很顯然,這個效果用PopupWindow來實現最合適。
下面通過一個Demo把這個需求做出來。效果圖:

先看布局文件。
1、主界面
我們只需要在右上角放一個按鈕來觸發PopupWindow。注意父容器要有一個id,因為後面要用它來給PopupWindow定位彈出位置:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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界面


Java代碼部分
首先自定義一個繼承自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:
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尚未創建完成。把它放到合適的時機(例如點擊事件觸發後)調用即可。
延伸閱讀:QQ空間實現(一)——展示說說中的評論內容並有相應點擊事件。
博主目前專注社交、社區類APP開發,歡迎同領域的朋友關注交流。
在 Android Studio 中配置 OpenCV 3.1 的完整教學
先到官網(https://opencv.org/)下載 OpenCV for Android 並解壓。由於網路因素,之前幾次下載都相當緩慢甚至中斷,後來某次意外順利完成。
單一 TextView 實現微信式回復評論:點擊換色與彈窗
想在一串回復文本中,點擊某一行就改變該行背景色並彈出對話框嗎?本文示範如何僅靠一個 TextView,實現類似微信回復評論的互動效果。 注意:這裡的多行回復文本都放在同
GitHub 好料分享:功能豐富的通知工具庫 NotifyUtil
GitHub 好料分享:功能豐富的通知工具庫 NotifyUtil 今天要介紹的是一個相當實用的通知工具庫 NotifyUtil。作者把常用的通知樣式都封裝得相當完整,使
Android 自定義控件入門:五個步驟拆解完整流程
Android 官方已經提供了琳瑯滿目的 View,但真實的產品需求千變萬化,現成元件总有覆蓋不到的時候。這時候就轮到自定義控件登場了。 很多初學者一听到「自定義控件」就