I have a DialogFragment
to show a View
like a Popup screen.
The Window appears always in the middle of the screen.
Is there a way to set the position of the DialogFragment
window?
I have looked in to the source code but couldn't find anything yet.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
getDialog().getWindow()
will not work for aDialogFragment
asgetWindow()
will return null if the hosting activity is not visible, which it isn't if you're writing a fragment-based app. You will get aNullPointerException
when you trygetAttributes()
.I recommend Mobistry's answer. If you already have a DialogFragment class, it's not too hard to switch over. Just replace the onCreateDialog method with one that constructs and returns a PopupWindow. Then you should be able to reuse the View you supply to
AlertDialog.builder.setView()
, and call(PopupWindow object).showAtLocation()
.Try something like this:
or other methods for
getDialog().getWindow()
.be sure to set the position after calling set-content.
Right, I banged head against wall for an hour or two with this, before finally getting
DialogFragment
positioned like I wanted.I'm building on Steelight's answer here. This is the simplest, most reliable approach I found.
Note that
params.width
andparams.softInputMode
(used in Steelight's answer) are irrelevant for this.Below is a more complete example. What I really needed was to align a "confirm box" DialogFragment next to a "source" or "parent" View, in my case an ImageButton.
I chose to use DialogFragment, instead of any custom Fragment, because it gives you "dialog" features for free (close dialog when user clicks outside of it, etc).
Example ConfirmBox above its "source" ImageButton (trashcan)
It's quite easy to make the above more general-use by adding constructor parameters or setters as necessary. (My final
ConfirmBox
has a styled button (inside some borders etc) whose text andView.OnClickListener
can be customised in code.)You need to override onResume() method in your DialogFragment like following:
I am using PopupWindow class for this purpose because you can specify the location and size of the view.