popup window like any.do

2019-04-09 11:32发布

i am writing an app that shows missed calls and unread sms in a pop up window. it also has a reminder feature (to close the pop up window and open it after a specified time). its similar to the popup window of any.do.

i was able to created such a window by using the WindowManger, but for some reasons which i dont understand so far, the pop up window disappears after some time (although it should be opened untill the user closes it, or clicks on the reminder button which could take hours).

this is how i created the existing popup window

windowManager = (WindowManager) context.getApplicationContext()
            .getSystemService(Context.WINDOW_SERVICE);              
    this.inflater = LayoutInflater.from(context);
    params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    + WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
                    + WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);
    params.gravity = Gravity.BOTTOM;
    vgPopupWindow = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.popup_mainwindow, null);
    vgPopupContent = (ViewFlipper) vgPopupWindow.findViewById(R.id.popup_content);
    vgPopupAreaContent = (ViewGroup) vgPopupWindow.findViewById(R.id.popup_area_content);
    vgPopupAreaActions = (ViewGroup) vgPopupWindow.findViewById(R.id.popup_area_actions);
    vgPopupTabs = (LinearLayout) vgPopupWindow.findViewById(R.id.popup_tabs);
    vgPopupHeader = (LinearLayout) vgPopupWindow.findViewById(R.id.popup_main_header);
    windowManager.addView(vgPopupWindow, params);

because i was not sure if the WindowManager is really the right way for that, i also tried to do the same with an activity with following style

<style name="PopupWindow" parent="CustomTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowFullscreen">false</item>
    <item name="android:windowIsTranslucent">true</item>

its looks now just like the WindowManager version, but has one problem: i can not access the window of my app below it, because the translucent parts of the window look perfect, but block any click events to the window below, so that i can not scroll it or do anything with it. like in any.do i just want that window to show up, but not to block any possibility to work on the window below while the popupwindow is opened.

EDIT: i want to see the pop up window at the bottom, covering 30% of the window, but be able to scroll the window under it (and also perform click events on it) at the same time.

so my questions are:

  1. is the WindowManager the right way to create this popupwindow?
  2. if yes, why does it disappear sometimes unwanted? how can i prevent it?
  3. if no, how can i create an activity that behaves like the window i created with the WindowManager?
  4. or are both ways wrong? in this case; whats the right way?

1条回答
叛逆
2楼-- · 2019-04-09 12:07

Here is the code I used to do the same thing

public class PopupActivity extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        AlertDialog.Builder adb = new Builder(this);
        AlertDialog dialogTest = adb.setTitle("Test title")
                .setMessage("Test message")
                .setNeutralButton("Button", new OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // do something
                        PopupActivity.this.finish();
                    }
                }).create();
        adb.setCancelable(false);
        dialogTest.show();
    }
}

and the XML style(originally posted here https://stackoverflow.com/a/2700683/995020)

<style name="Theme.Transparent" parent="AppTheme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
</style>

Set the style for the activity in the manifest file. And then just start the activity any time you want to show the popup window. You may add EXTRA data to an intent and use it for customizing the popup.

查看更多
登录 后发表回答