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);
    }
}

















  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值