彈出PopupWindow背景變暗的實現


       彈出PopuoWindow后 代碼里設置的是PopupWindow默認獲取焦點 所以PopupWindow顯示的時候其它控件點擊是沒有反應的

用到的方法是

pwMyPopWindow.setFocusable(true);

代碼里還設置了

pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
R.mipmap.ic_launcher));// 設置背景圖片,不能在布局中設置,要通過代碼來設置
pwMyPopWindow.setOutsideTouchable(true);// 觸摸popupwindow外部,popupwindow消失。這個要求你的popupwindow要有背景圖片才可以成功
這樣點擊PopupWindow外部就會自行消失

當我們彈出PopupWindow的時候讓背景變半透明 當PopuuWindow消失時讓背景變回原樣 這樣就實現了多數app中PouupWindow的使用效果
用到的方法是
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);

下面是代碼
<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:background="#ffffff" >

    <LinearLayout
        android:id="@+id/ll_head_bar"
        android:layout_width="wrap_content"
        android:layout_height="400dp"
        android:orientation="vertical">
   <!--這個圖片按鈕按下時彈出PopupWindow-->
        <ImageButton
            android:id="@+id/ib_operate_more"
           android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/ic_launcher" />
<!--這個按鈕是測試當PoupWindow顯示時點擊此按鈕沒反應 因為此按鈕沒獲取焦點--> <Button android:layout_marginTop="200dp" android:layout_gravity="bottom" android:id="@+id/ib_operate_more2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="測試按鈕" /> </LinearLayout> </RelativeLayout>
 
        
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_list_item"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:minHeight="40dp"
        android:minWidth="120dp"
        android:textSize="20sp"
        />
</LinearLayout>
list_item_popupwindow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#d93c58b3"
    >"

    <ListView
        android:id="@+id/lv_popup_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="#00000000"
        android:focusableInTouchMode="true"
        android:background="#d42a2a"
        />

</LinearLayout>
task_detail_popupwindow.xml

 

package com.ceshi.popupwindow;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends Activity {

    private ImageButton ibOperationMore;
    private Button ibOperationMore2;

    List<Map<String, String>> moreList;
    private PopupWindow pwMyPopWindow;// popupwindow
    private ListView lvPopupList;// popupwindow中的ListView
    private int NUM_OF_VISIBLE_LIST_ROWS = 3;// 指定popupwindow中Item的數量

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iniData();

        iniPopupWindow();

        // 更多操作按鈕
        ibOperationMore = (ImageButton) findViewById(R.id.ib_operate_more);
        //ibOperationMore2是用來測試焦點問題  這樣popupwindow顯示的時候其他的控件是不能點擊的
        //換句話說其它控件點擊時沒反應
        ibOperationMore2 = (Button) findViewById(R.id.ib_operate_more2);
        ibOperationMore2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            Toast.makeText(MainActivity.this,"cao",Toast.LENGTH_SHORT).show();
            }

        });

        ibOperationMore.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (pwMyPopWindow.isShowing()) {
                    pwMyPopWindow.dismiss();// 關閉
                } else {
                    pwMyPopWindow.showAsDropDown(ibOperationMore);// 顯示
                    backgroundAlpha(0.7f);
                }

            }
        });
    }

    private void iniData() {

        moreList = new ArrayList<Map<String, String>>();
        Map<String, String> map;
        map = new HashMap<String, String>();
        map.put("share_key", "復制");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "刪除");
        moreList.add(map);
        map = new HashMap<String, String>();
        map.put("share_key", "修改");
        moreList.add(map);
    }

    private void iniPopupWindow() {

        LayoutInflater inflater = (LayoutInflater) this
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
        lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
        pwMyPopWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        pwMyPopWindow.setFocusable(true);// 加上這個popupwindow中的ListView才可以接收點擊事件

        lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList,
                R.layout.list_item_popupwindow, new String[] { "share_key" },
                new int[] { R.id.tv_list_item }));
        lvPopupList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                pwMyPopWindow.dismiss();
                Toast.makeText(MainActivity.this,
                        moreList.get(position).get("share_key"),
                        Toast.LENGTH_LONG).show();
            }
        });

        // 控制popupwindow的寬度和高度自適應
        /*lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
                View.MeasureSpec.UNSPECIFIED);
        pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
        pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)
                * NUM_OF_VISIBLE_LIST_ROWS);*/

        // 控制popupwindow點擊屏幕其他地方消失
        pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
                R.mipmap.ic_launcher));// 設置背景圖片,不能在布局中設置,要通過代碼來設置
        pwMyPopWindow.setOutsideTouchable(true);// 觸摸popupwindow外部,popupwindow消失。這個要求你的popupwindow要有背景圖片才可以成功,如上
        pwMyPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                //popupwindow消失的時候恢復成原來的透明度
                backgroundAlpha(1f);
            }
        });

    }

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

效果圖



嚴禁盜版    

轉載請注明出處:https://www.cnblogs.com/bimingcong/p/4982237.html

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM