Android 自定义 PopupWindow/Dialog

时间:2023-03-21
本文介绍了Android 自定义 PopupWindow/Dialog的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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.

最好是通过从 XML 扩展视图来实现,但在这一点上,任何可以正常工作的东西都会很好.

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

谢谢.

推荐答案

我采取的步骤:

  1. 创建一个扩展Dialog的类.
  2. 在 onCreate 中,调用 setContentView(x, y),其中 x 是您的 R.layout,y 是 R.style.popupStyle(见下文).
  3. 在您的 res/values/style.xml 中,您需要覆盖默认的 DialogWindow 样式.我尝试制作一种以该样式为父样式的样式,但这仍然没有清除所有默认值.所以我检查了 Android git 树并得到了默认样式,然后复制粘贴了它.就是这个:
  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>

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

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. :)

感谢 mbaird 让我走上正轨.

Thanks to mbaird for putting me on the right track.

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

[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.

这篇关于Android 自定义 PopupWindow/Dialog的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何在 Android 中创建完全自定义的 Dialogue/Popup(更改覆盖颜色和对话窗口布局) 下一篇:Android 无边框对话框

相关文章