PopupWindow的背景变暗不包括工具栏/动作条 [英] Dimmed Background of PopupWindow excludes Toolbars / Action Bars

查看:1183
本文介绍了PopupWindow的背景变暗不包括工具栏/动作条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的codeS来创建一个 PopupWindow 通过点击工具栏菜单项昏暗的背景

  @覆盖
公共布尔onOptionsItemSelected(菜单项项){
    开关(item.getItemId()){
        案例R.id.btn_1:
            Log.i(TAG,点击的按钮1);
            LayoutInflater layoutInflater =(LayoutInflater)getBaseContext()getSystemService(LAYOUT_INFLATER_SERVICE)。
            查看popupView = layoutInflater.inflate(R.layout.popup_window,NULL);            最后PopupWindow popupWindow =新PopupWindow(popupView,Toolbar.LayoutParams.MATCH_PARENT,Toolbar.LayoutParams.MATCH_PARENT);
            按钮btnDismiss =(按钮)popupView.findViewById(R.id.btn_dismiss);
            btnDismiss.setOnClickListener(新View.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    popupWindow.dismiss();
                }
            });
            popupWindow.showAtLocation(popupView,Gravity.CENTER,0,0);
            返回true;
        默认:
            返回super.onOptionsItemSelected(项目);
    }
}

不过,我想昏暗的背景排除的动作条/工具栏。我怎样才能实现呢?

的布局 PopupView 情况如下:

 <?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直的android:layout_width =match_parent
    机器人:layout_height =match_parent>
    < RelativeLayout的
        机器人:ID =@ + ID / bac_dim_layout
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:背景=#C0000000
        机器人:知名度=看得见>
        <的LinearLayout
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:方向=垂直
            机器人:layout_margin =1DP
            机器人:背景=@机器人:彩色/ darker_gray>
            >
            <的LinearLayout
                机器人:layout_width =WRAP_CONTENT
                机器人:layout_height =WRAP_CONTENT
                机器人:方向=垂直
                机器人:layout_margin =20dp>
                <的TextView
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:文字=这是一个PopupWindow/>
                <按钮
                    机器人:ID =@ + ID / btn_dismiss
                    机器人:layout_width =FILL_PARENT
                    机器人:layout_height =WRAP_CONTENT
                    机器人:文字=辞退/>
            < / LinearLayout中>
        < / LinearLayout中>
    < / RelativeLayout的>
< / LinearLayout中>


解决方案

如果你想弹出窗口,除了工具栏/动作条

暗淡的背景显示

在这里,我尝试设置弹出窗口的位置。取状态栏和放大器;导航栏的高度和放大器;设置Y位置到弹出

  INT actionBarHeight = 0;
                查看popupView = LayoutInflater.from(getActivity())膨胀(R.layout.popup_,NULL);
                最后PopupWindow popupWindow =新PopupWindow(popupView,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
                actionBarHeight = getNavigationBarHeight(getActivity(),Configuration.ORIENTATION_PORTRAIT);
                INT RESOURCEID = getResources()则getIdentifier(status_bar_height,扪,机器人)。
                如果(RESOURCEID大于0){
                    actionBarHeight = actionBarHeight + getResources()getDimensionPixelSize(RESOURCEID)。
                }
                按钮btnDismiss =(按钮)popupView.findViewById(R.id.btn_dismiss);
                btnDismiss.setOnClickListener(新View.OnClickListener(){
                    @覆盖
                    公共无效的onClick(视图v){
                        popupWindow.dismiss();
                    }
                });
                popupWindow.showAsDropDown(popupView,0,actionBarHeight);

另一种方式如你所说的comment.Below提到你可以尝试显示弹出

参加布局,将对准顶部一个隐藏控件(TextView中)。就拿的TextView和放大器的位置;分配Y位置,以弹出式窗口

 公共类MyPopupTest扩展片段{
    公共静态最后的字符串标记=MyPopupTest;    @覆盖
    公共无效的onCreate(@Nullable捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
    }    @Nullable
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.home_screen_slider,集装箱,FALSE);
    }
    @覆盖
    公共无效onViewCreated(最终查看视图,@Nullable捆绑savedInstanceState){
        super.onViewCreated(查看,savedInstanceState);
        view.findViewById(R.id.btnSelect).setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                showPopup(视图);
            }
        });
    }    公共无效showPopup(查看视图){
        查看popupView = LayoutInflater.from(getActivity())膨胀(R.layout.popup_,NULL);
        最后PopupWindow popupWindow =新PopupWindow(popupView,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT);
        按钮btnDismiss =(按钮)popupView.findViewById(R.id.btn_dismiss);
        btnDismiss.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                popupWindow.dismiss();
            }
        });
        INT [] locationOfHiddenTextView =新INT [2];
        view.findViewById(R.id.hiddenTopTextView).getLocationOnScreen(locationOfHiddenTextView);
        //给位置为起点的弹出
        popupWindow.showAsDropDown(popupView,0,locationOfHiddenTextView [1]);
    }
}

这是goint显示片段的XML文件

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@ + ID / relative_home
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:背景=#ffc699>
    <的TextView
        机器人:ID =@ + ID / hiddenTopTextView
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentTop =真
        机器人:知名度=水涨船高/>    <按钮
        机器人:ID =@ + ID / btnSelect
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=显示弹出/>< / RelativeLayout的>

I use the following codes to create a PopupWindow with dim background by clicking a menu item in Toolbar:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.btn_1:
            Log.i(TAG, "Clicked Button 1");
            LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup_window, null);

            final PopupWindow popupWindow = new PopupWindow(popupView, Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.MATCH_PARENT);
            Button btnDismiss = (Button)popupView.findViewById(R.id.btn_dismiss);
            btnDismiss.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });
            popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 0);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

However, I want to the dim background to exclude the action bars / toolbars. How can I achieve it?

The layout of the PopupView is as follow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RelativeLayout
        android:id="@+id/bac_dim_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C0000000"
        android:visibility="visible" >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_margin="1dp"
            android:background="@android:color/darker_gray">
            >
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="20dp">
                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="It's a PopupWindow" />
                <Button
                    android:id="@+id/btn_dismiss"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Dismiss" />
            </LinearLayout>
        </LinearLayout>
    </RelativeLayout>
</LinearLayout>

解决方案

if you want popup window to display with dim background except ToolBar/ActionBar

here i tried setting position of popup window. fetch statusbar & navigation bar height & set Y position to the popup

int actionBarHeight = 0;
                View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_, null);
                final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
                actionBarHeight = getNavigationBarHeight(getActivity(), Configuration.ORIENTATION_PORTRAIT);
                int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
                if (resourceId > 0) {
                    actionBarHeight = actionBarHeight + getResources().getDimensionPixelSize(resourceId);
                }
                Button btnDismiss = (Button) popupView.findViewById(R.id.btn_dismiss);
                btnDismiss.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popupWindow.dismiss();
                    }
                });
                popupWindow.showAsDropDown(popupView, 0, actionBarHeight);

Another way to display popup as you said in comment.Below mentioned you can try

Take one hidden control(TextView) in layout which will be aligned to top. Take the location of the TextView & assigned Y position to popup window

public class MyPopupTest extends Fragment{
    public static final String TAG = "MyPopupTest";

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.home_screen_slider, container, false);
    }


    @Override
    public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        view.findViewById(R.id.btnSelect).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopup(view);
            }
        });
    }

    public void showPopup(View view) {
        View popupView = LayoutInflater.from(getActivity()).inflate(R.layout.popup_, null);
        final PopupWindow popupWindow = new PopupWindow(popupView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
        Button btnDismiss = (Button) popupView.findViewById(R.id.btn_dismiss);
        btnDismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        int[] locationOfHiddenTextView = new int[2];
        view.findViewById(R.id.hiddenTopTextView).getLocationOnScreen(locationOfHiddenTextView);
        //Give Position as a start point for popup
        popupWindow.showAsDropDown(popupView, 0, locationOfHiddenTextView[1]);
    }
}

XML File of the fragment which is goint to display

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffc699">


    <TextView
        android:id="@+id/hiddenTopTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:visibility="gone" />

    <Button
        android:id="@+id/btnSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Popup" />

</RelativeLayout>

这篇关于PopupWindow的背景变暗不包括工具栏/动作条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆