Activity设置为对话框属性时(Theme.Dialog)时,改变其在屏幕中的位置
16lz
2021-01-25
原帖http://stackoverflow.com/questions/2163427/android-dialog-activity-position
如果有需要要将Activity变成一个窗口形式(在Manifest.xml中的activity标签设置android:theme="@android:style/Theme.Dialog"属性),默认Activity窗口弹出是在屏幕的正中央。通过覆写Activity中onAttachedToWindow()方法,在期中将DecorView的参数重设一下就可以指定其显示的位置了。
@Overridepublic 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);}
lp.x , lp.y , lp.width ,lp.height的具体值可以定义到res/values/dimens.xml中,方便以后修改。
@Override public void onAttachedToWindow() { // 设置本Activity在父窗口的位置 super.onAttachedToWindow(); View view = getWindow().getDecorView(); WindowManager.LayoutParams lp = (WindowManager.LayoutParams) view .getLayoutParams(); lp.gravity = Gravity.RIGHT | Gravity.BOTTOM; lp.x = getResources().getDimensionPixelSize( R.dimen.playqueue_dialog_marginright); lp.y = getResources().getDimensionPixelSize( R.dimen.playqueue_dialog_marginbottom); lp.width = getResources().getDimensionPixelSize( R.dimen.playqueue_dialog_width); lp.height = getResources().getDimensionPixelSize( R.dimen.playqueue_dialog_height); getWindowManager().updateViewLayout(view, lp); }
<resources> <dimen name="playqueue_dialog_marginright">0dp</dimen> <dimen name="playqueue_dialog_marginbottom">60dp</dimen> <dimen name="playqueue_dialog_width">250dp</dimen> <dimen name="playqueue_dialog_height">380dp</dimen> <dimen name="playqueue_dialog_select_item_from_top">90dp</dimen></resources>
更多相关文章
- Android(安卓)N(7.0) 遇到 android.os.FileUriExposedException: f
- Android(安卓)SurfaceFlinger原理详解
- ANDROID GUI
- Android沉浸式状态栏 + actionBar渐变 + scrollView顶部伸缩
- Android悬浮窗用法总结
- 注册谷歌地图 API 密钥和显示谷歌地图
- 怎么解决这个问题“The connection to adb is down, and a sever
- 8.3facebook分享后不回调结果原因,java标签代码
- Android(安卓)LocationManager 使用