1. 程式人生 > >PopupWindow彈出後螢幕其他部分變暗

PopupWindow彈出後螢幕其他部分變暗

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">由於平時用PopupWindow的機會很少,然後今天產品要一個需求的時候居然一時沒轍了:PopupWindow在彈出來的時候螢幕的其他部分要變暗</span></span>

看了下PopupWindow也沒有這樣的屬性,然後只好Google了,看著各種五花八門的方式,

一會是把PopupWindow鋪滿整個螢幕然後設定背景了,

一會又是用dialog的方式實現了,這些如果對於PopupWindow彈出的位置沒有特別需求的話是沒有問題,

但是如果要讓PopupWindow顯示到某個控制元件的下面,

我就不知道以上方式怎麼去對齊了!然後還有一種方式是通過getwindow()設定alpha的方式,這種方式在某些手機上會閃屏挺煩的。除了這些,就沒找到其他的方式了。

後來實在沒轍了,只能使用一種笨方法了,那就是activity的佈局採用FrameLayout來做,然後隱藏一個鋪滿整個螢幕的view,背景為暗色,當PopupWindow顯示的時候這個view也顯示,當PopupWindow消失的時候這個view也隱藏。具體如下:

activity.xml

<FrameLayout 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" >

    <View
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#60000000" 
        android:visibility="gone"
        />
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.gradletest.MainActivity" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@+id/textView1"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="38dp"
            android:text="Button" />
    </RelativeLayout>

</FrameLayout>
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="match_parent"
    android:gravity="center"
    android:background="#FFFFFF"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

public class MainActivity extends Activity {

	private View view;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        view = findViewById(R.id.view);
        Button btn = (Button) findViewById(R.id.button1);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupWindow popupWindow = initPopupWindow();
                popupWindow.showAsDropDown(v);
                view.setVisibility(View.VISIBLE);
            }
        });
    }


    /**
     * 建立PopupWindow
     * 
     * @param popView
     *            指定PopupWindow的內容
     */
    private PopupWindow createPopupWindow(View popView) {
     
        PopupWindow popupInstance = new PopupWindow(popView, LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT);
        popupInstance.setFocusable(true);
        popupInstance.setBackgroundDrawable(new ColorDrawable(0));
        popupInstance.setOutsideTouchable(true);
        // 監聽器
        popupInstance.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
            	view.setVisibility(View.GONE);
            }
        });
     
        return popupInstance;
    }
    
    /**
     * 初始化rightPopupWindow
     */
    private PopupWindow initPopupWindow() {
     
        LayoutInflater inflater = LayoutInflater.from(this);
        View popView = inflater.inflate(R.layout.popwindow, null);
        // 建立PopupWindow
        final PopupWindow popupWindow = createPopupWindow(popView);
        return popupWindow;
    } 
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}