1. 程式人生 > >Android 自定義PopupWindow實現懸浮窗效果

Android 自定義PopupWindow實現懸浮窗效果

  有時候我們需要在介面上彈出一個視窗,而Android中彈出窗體有兩種方式:一種是AlertDialog,另一種就是PopupWindow,AlertDialog的位置是固定的,而PopupWindow的位置可以任意指定。下面我們使用自定義的PopupWindow來完成以下的效果圖:這裡寫圖片描述
  

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import
android.widget.PopupWindow; /** * 自定義懸浮窗PopupWindow */ public class CustomPopupWindow implements PopupWindow.OnDismissListener { private PopupWindowListener mPopupWindowListener; private PopupWindow mPopupWindow; private Activity mActivity; private View mParentView; private int
mWidth; private int mHeight; private View mPopupWindowView; private boolean focusable; public CustomPopupWindow(View parentView, Activity activity, View contentView, int width, int height, boolean focusable) { this.mActivity = activity; this.mParentView = parentView; this
.mWidth = width; this.mHeight = height; this.focusable = focusable; this.mPopupWindowView = contentView; } /** * 顯示PopupWindow */ public void showView() { mPopupWindow = new PopupWindow(mPopupWindowView, mWidth, mHeight, focusable); if (mPopupWindowListener != null) { mPopupWindowListener.initView(); } mPopupWindow.setBackgroundDrawable(new ColorDrawable(0xFFFFFF)); mPopupWindow.showAtLocation(mParentView, Gravity.CENTER | Gravity.CENTER, 0, 0); mPopupWindow.update(); mPopupWindow.setOnDismissListener(this); } /** * 點選懸浮窗外面時的操作 */ @Override public void onDismiss() { setBackgroundAlpha(1f); } public interface PopupWindowListener { // 初始化PopupWindow的控制元件 void initView(); } public void setOnPopupWindowListener(PopupWindowListener listener) { this.mPopupWindowListener = listener; } /** * 隱藏PopupWindow */ public void dismiss() { if (mPopupWindow != null) { mPopupWindow.dismiss(); mPopupWindow = null; } } //設定螢幕背景透明效果 public void setBackgroundAlpha(float alpha) { WindowManager.LayoutParams lp = mActivity.getWindow().getAttributes(); mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); lp.alpha = alpha; mActivity.getWindow().setAttributes(lp); } }

  在MainActivity中:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private CustomPopupWindow mCustomPopupWindow;
    private ImageButton mImageButton;//懸浮窗的關閉按鈕
    private View mLayoutPopupWindowView;//懸浮窗的佈局
    private TextView mTvActivityRule;//懸浮窗的內容
    private Button button;//點選懸浮窗的按鈕

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button)findViewById(R.id.btn_activity_rule);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_activity_rule:
                showPopupWindow();
                break;
            //關閉懸浮窗
            case R.id.popupwindow_activity_rule_imgBtn:
                mCustomPopupWindow.dismiss();
                mCustomPopupWindow.setBackgroundAlpha(1);
                break;
        }
    }

    /**
     * 顯示懸浮窗,並設定背景透明度
     */
    public void showPopupWindow() {
        mLayoutPopupWindowView = LayoutInflater.from(this).inflate(R.layout
                .popupwindow_activity_rule, null);
        mCustomPopupWindow = new CustomPopupWindow(findViewById(R.id.linearlayout),
                this, mLayoutPopupWindowView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout
                .LayoutParams.WRAP_CONTENT, true);
        mCustomPopupWindow.setOnPopupWindowListener(new CustomPopupWindow
                .PopupWindowListener() {

            // TODO 設定活動內容
            @Override
            public void initView() {
                mImageButton = (ImageButton) mLayoutPopupWindowView.findViewById(R.id
                        .popupwindow_activity_rule_imgBtn);
                mImageButton.setOnClickListener(MainActivity.this);
                mTvActivityRule = (TextView) mLayoutPopupWindowView.findViewById(R.id
                        .popupwindow_activity_rule_text);
                mTvActivityRule.setText("");
            }
        });
        mCustomPopupWindow.showView();
        mCustomPopupWindow.setBackgroundAlpha(0.3f);
    }
}

  其中popupwindow_activity_rule的佈局為:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:background="#FFFFFF"
              android:orientation="vertical">

    <ScrollView
        android:layout_width="300dp"
        android:layout_height="400dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_margin="5dp"
                    android:text="活動規則"
                    android:textColor="#000000"
                    android:textSize="18sp"/>
                <ImageButton
                    android:id="@+id/popupwindow_activity_rule_imgBtn"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:layout_alignParentRight="true"
                    android:background="@null"/>
            </RelativeLayout>
            <TextView
                android:id="@+id/popupwindow_activity_rule_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:textSize="14sp"/>
        </LinearLayout>
    </ScrollView>
</LinearLayout>

  其中ImageButton沒有新增圖片,可自行新增;
  activity_main中新增一個按鈕:R.id.btn_activity_rule,整個父窗體id為R.id.linearlayout;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF0000"
    android:orientation="vertical">                   
            <Button
                android:id="@+id/btn_activity_rule"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="20dp"        
                android:text="活動規則"
                android:textSize="18sp"/>
</LinearLayout>