Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android PopupWindow 仿微信底部彈出效果

編輯:關於 Android 程式設計

專案中 PopupWindow 的使用頻率很高,是相當實用的元件。本文示範一款仿微信風格的底部彈出視窗,可直接套用到專案。先看成果:

一、彈出視窗版面

版面相當簡單,以 LinearLayout 垂直排列數顆按鈕:

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:id="@+id/pop_layout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bg_pulish" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/btn_hand" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:background="@drawable/bg_publish_product_selector" android:text="1" /> <Button android:id="@+id/btn_library" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:background="@drawable/bg_publish_product_selector" android:text="2" /> <Button android:id="@+id/btn_two_code" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="20dip" android:background="@drawable/bg_publish_product_selector" android:text="3" /> <Button android:id="@+id/btn_cancel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dip" android:layout_marginRight="20dip" android:layout_marginTop="30dip" android:layout_marginBottom="30dp" android:background="@drawable/bg_publish_product_selector" android:text="取消" /> </LinearLayout>

二、核心程式碼

自訂 PublishSelectPicPopupWindow 繼承 PopupWindow,設定寬高、焦點、動畫與背景,並處理點擊事件:

package com.example.showpopupwindow; import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.PopupWindow; public class PublishSelectPicPopupWindow extends PopupWindow { private Button btnHand; private Button btnLibrary; private Button btnTwocode; private Button btnCancel; private View mMenuView; @SuppressWarnings("deprecation") public PublishSelectPicPopupWindow(Activity context, OnClickListener itemsOnClick) { super(context); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mMenuView = inflater.inflate(R.layout.publish_dialog, null); btnHand = (Button) mMenuView.findViewById(R.id.btn_hand); btnLibrary = (Button) mMenuView.findViewById(R.id.btn_library); btnTwocode = (Button) mMenuView.findViewById(R.id.btn_two_code); btnCancel = (Button) mMenuView.findViewById(R.id.btn_cancel); btnHand.setOnClickListener(itemsOnClick); btnLibrary.setOnClickListener(itemsOnClick); btnTwocode.setOnClickListener(itemsOnClick); btnCancel.setOnClickListener(itemsOnClick); this.setContentView(mMenuView); this.setWidth(LayoutParams.FILL_PARENT); this.setHeight(LayoutParams.WRAP_CONTENT); this.setFocusable(true); this.setAnimationStyle(R.style.AnimBottom); ColorDrawable dw = new ColorDrawable(0xb0000000); this.setBackgroundDrawable(dw); mMenuView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { int height = mMenuView.findViewById(R.id.pop_layout).getTop(); int y = (int) event.getY(); if (event.getAction() == MotionEvent.ACTION_UP) { if (y < height) { dismiss(); } } return true; } }); } }

三、在 Activity 中呼叫

建立 PopupWindow 實例,以 showAtLocation 對齊底部顯示,並在監聽器中處理各按鈕:

package com.example.showpopupwindow; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private PublishSelectPicPopupWindow menuWindow; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { menuWindow = new PublishSelectPicPopupWindow(MainActivity.this, itemsOnClick); menuWindow.showAtLocation(MainActivity.this.findViewById(R.id.button1), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); } }); } private OnClickListener itemsOnClick = new OnClickListener() { public void onClick(View v) { menuWindow.dismiss(); switch (v.getId()) { case R.id.btn_hand: Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show(); break; case R.id.btn_two_code: Toast.makeText(MainActivity.this, "2", Toast.LENGTH_SHORT).show(); break; case R.id.btn_library: Toast.makeText(MainActivity.this, "3", Toast.LENGTH_SHORT).show(); break; default: break; } } }; }

圖片資源隨原始碼一併提供,下載後即可直接套用,簡單又高效。

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