Android:如何制作模态自定义视图?
我想制作自己的对话框,完全由我自己绘制,作为自定义视图。我可以以这样的方式显示它:它显示在所有其他视图之上,并且所有触摸事件(甚至是对话框区域之外的事件)都重定向到对话框吗?
I would like to make my own dialog, completely drawn myself, as a custom view. Can I display it in such a way that it displays above all other views and that all touch events, even those outside the dialog's area, are redirected to the dialog?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我发现使用透明的活动和 startActivityForResult() 给了我完全的自由,我希望我的“对话框”的外观和行为如何。所以我建议你看看:如何在 Android 上创建透明 Activity?
使用全屏和您选择的较暗背景:
然后在 strings.xml 中:
如果您想模糊上一个 Activity 的屏幕,请在 setContentView 之前使用此行:
I find that using a transparent activity and startActivityForResult() gives me a total freedom how I want my "dialog" to look and behave. So I suggest you check it out: How do I create a transparent Activity on Android?
With full screen and a darker background of your choice:
Then in strings.xml:
If you want to blur the screen of the previous activity then use this line before setContentView:
这应该可以通过将根元素的高度/宽度设置为除
fill_parent
之外的任何值来实现。您还可以通过创建继承内置 Android 对话框主题的自定义主题来实现此目的。
http://developer.android.com/guide/topics/ui/themes.html
This should be possible by setting the height/width of the root element to anything but
fill_parent
.You could also achieve this by creating a custom theme that inherits from the built in android dialog theme.
http://developer.android.com/guide/topics/ui/themes.html
这是我的解决方法。
首先,使用
Window#getDecorView().findViewById(android.R.id.content)
获取装饰视图的内容子视图,它是一个FrameLayout
。然后,将您的自定义视图添加到此
FrameLayout
中,它将位于其他视图的顶部。Here's my workaround.
First, obtain the decor view's content child view, which is a
FrameLayout
, usingWindow#getDecorView().findViewById(android.R.id.content)
.Then, add your custom view to this
FrameLayout
and it will be at the top of other views.