android dialog activity position

前端 未结 4 673
说谎
说谎 2020-12-24 08:07

I created an non-maximized activity using android:theme=\"@android:style/Theme.Dialog\" to make it looks like a dialog. I need to change the position of the activity on scre

相关标签:
4条回答
  • 2020-12-24 08:44

    To change the position (with the theme set to Theme.Dialog), you can override the gravity, size and coordinates of the LayoutParams of your activity's decor view after it has been attached to the window. Here's an example:

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
    
        View view = getWindow().getDecorView();
        WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view.getLayoutParams();
        lp.gravity = Gravity.LEFT | Gravity.TOP;
        lp.x = 10;
        lp.y = 10;
        lp.width = 300;
        lp.height = 300;
        getWindowManager().updateViewLayout(view, lp);
    }
    
    0 讨论(0)
  • 2020-12-24 08:54

    Create a custom theme with Theme.Dialog as its parent:

      <style name="CustomDialogTheme" parent="@android:style/Theme.Dialog">
    

    So, for each item in the Dialog theme that you want to change, use CustomDialogTheme instead of Theme.Dialog inside the Android Manifest. See the android developper docs for details.

    0 讨论(0)
  • 2020-12-24 09:06

    For bottom:

    Keep this in your activity.

    @Override
        public void onAttachedToWindow() {
            super.onAttachedToWindow();
            try {
                View view = getWindow().getDecorView();
                WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) view.getLayoutParams();
                layoutParams.gravity = Gravity.BOTTOM;
                getWindowManager().updateViewLayout(view, layoutParams);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    

    Note: You can change your gravity by setting this Gravity.BOTTOM (TOP, LEFT, RIGHT)

    0 讨论(0)
  • 2020-12-24 09:09

    Use .setGravity()

    progDialog = ProgressDialog.show();

    progDialog.getWindow().setGravity(Gravity.BOTTOM);

    0 讨论(0)
提交回复
热议问题