Android 自定义弹出窗口/对话框

发布于 2024-08-31 13:08:39 字数 189 浏览 6 评论 0原文

我试图获得一个完全自定义的 Dialog 或 PopupWindow,没有任何默认的 Android UI 控件(标题、背景、按钮等)。

这有可能吗?我花了几个小时寻找这个,但没有运气......看起来这应该很容易实现,但我找不到它。

最好是通过从 XML 扩充视图来实现,但此时任何能正常工作的东西都很好。

谢谢。

I'm trying to get a completely custom Dialog or PopupWindow, without any of the default Android UI controls (title, background, buttons, whatever).

Is this possible at all? I've spent hours searching for this, but no luck... It seems like this should be easily possible, but I can't find it.

Preferably this would be by inflating a View from XML, but at this point anything that would just work would be nice.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆光下的微笑 2024-09-07 13:08:39

我采取的步骤:

  1. 创建一个扩展 Dialog 的类。
  2. 在 onCreate 中,调用 setContentView(x, y),其中 x 是您的 R.layout,y 是 R.style.popupStyle(见下文) )。
  3. 在 res/values/style.xml 中,您需要覆盖默认的 DialogWindow 样式。我尝试制作一种以该样式作为其父级的样式,但这仍然没有清除所有默认值。所以我检查了 Android git 树并获得了默认样式,然后复制粘贴了它。就是这样:
<style name="Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
    <item name="android:windowBackground">@android:drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

您会遇到一些错误,只需从官方 Android styles.xml 和 theme.xml 文件中复制更多内容即可解决它们。这是我的 styles.xml 文件的内容: http://pastebin.com/RRR15YYS

这只是给你一个白色弹出窗口,没有边框,什么都没有。开始定制。 :)

感谢 mbaird 让我走上了正确的道路。

[编辑]我需要再次查找我自己的答案,我花了至少十分钟搜索官方的 android 样式/主题文件,所以这里是,以供将来参考:
styles.xmlthemes.xml

Steps I took:

  1. Create a class extending Dialog.
  2. In the onCreate, call setContentView(x, y) with x being your R.layout and y being R.style.popupStyle (see below).
  3. In your res/values/style.xml, you need to override the default DialogWindow style. I tried just making a style that has this one as its parent, but that still didn't clear all defaults. So I checked the Android git tree and got the default style, and just copy-pasted it. This is the one:
<style name="Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowTitleStyle">@android:style/DialogWindowTitle</item>
    <item name="android:windowBackground">@android:drawable/panel_background</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>

You'll get a few errors, just solve them by copying more stuff from the official Android styles.xml and themes.xml files. Here's the contents of my styles.xml file: http://pastebin.com/RRR15YYS

That just gives you a white popup, no borders, nothing. Start customizing. :)

Thanks to mbaird for putting me on the right track.

[edit] I needed to look up my own answer again, and I spent at least ten minutes searching the official android styles/themes files, so here they are, for future reference:
styles.xml and themes.xml.

离线来电— 2024-09-07 13:08:39

听起来您正在尝试真正自定义 AlertDialog< /a>.对于您想做的事情,您最好创建自己的类来扩展 对话框,类似于通过编写扩展 活动

您可以通过在自定义 Dialog 类的 onCreate() 方法中调用 setContentView() 来设置布局 XML,就像在 Activity 中一样。

过去,我遇到了对 AlertDialogs 的自定义程度的限制,并且我刚刚实现了自己的 Dialog 类以获得我需要的自定义级别。

It sounds like you are trying to really customize an AlertDialog. For what you are wanting to do you may be better off just creating your own class that extends Dialog, similar to how you create activities by writing a class that extends Activity.

You can set the layout XML by calling setContentView() inside the onCreate() method of your custom Dialog class, just like you would in an Activity.

I've run into limitations on how much you can customize AlertDialogs in the past, and I've just implemented my own Dialog classes to get the level of customization that I needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文