PopupWindow的变暗背景不包括工具栏/操作栏

猛禽

PopupWindow通过单击以下菜单项使用以下代码创建背景较暗的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);
    }
}

但是,我要在昏暗的背景下排除动作栏/工具栏。我该如何实现?

的布局PopupView如下:

<?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>
用户名

如果您希望弹出窗口以暗淡的背景显示,除了ToolBar / ActionBar

在这里,我尝试设置弹出窗口的位置。获取状态栏和导航栏的高度并将Y位置设置为弹出窗口

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

如您在评论中所述,显示弹出窗口的另一种方法。

在布局中采用一个隐藏的控件(TextView),该控件将与顶部对齐。将TextView的位置和分配给Y的位置弹出窗口

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文件

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

工具栏文字变暗

来自分类Dev

如何使qterminal的工具栏和标签栏变暗?

来自分类Dev

Android工具栏,而不是操作栏

来自分类Dev

Android工具栏,而不是操作栏

来自分类Dev

用工具栏替换操作栏

来自分类Dev

如何使支持工具栏背景透明?

来自分类Dev

温泉工具栏背景颜色不变

来自分类Dev

温泉工具栏背景颜色不变

来自分类Dev

安卓工具栏背景

来自分类Dev

在工具栏/操作栏中更改文本颜色,Android?

来自分类Dev

工具栏和操作栏未显示在活动中

来自分类Dev

隐藏操作栏/工具栏时使内容动画化

来自分类Dev

将工具栏设置为片段中的操作栏

来自分类Dev

用工具栏替换操作栏后出现NullPointerException

来自分类Dev

Android隐藏操作栏和工具栏

来自分类Dev

操作栏中的工具栏选项?(扑)

来自分类Dev

在操作栏/工具栏上展开导航抽屉

来自分类Dev

在工具栏或操作栏的中心设置ActionBarDrawerToggle,Android吗?

来自分类Dev

用工具栏替换操作栏后出现NullPointerException

来自分类Dev

Android Studio工具栏为操作栏错误

来自分类Dev

上下文工具栏操作模式在API 16/19上具有白色背景

来自分类Dev

工具栏盲区

来自分类Dev

Javascript工具栏

来自分类Dev

Android:如何获取视图坐标(不包括操作栏)

来自分类Dev

如何将布局背景合并到工具栏

来自分类Dev

如何更改工具栏选项背景的颜色

来自分类Dev

工具栏选项菜单背景色

来自分类Dev

AppCompat样式背景传播到工具栏中的图像

来自分类Dev

更改工具栏背景颜色和标题