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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2773502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:18:03  来源:igfitidea点击:

Android Custom PopupWindow/Dialog

androiddialogskinningpopupwindow

提问by benvd

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

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

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.

最好是通过从 XML 膨胀一个视图,但在这一点上,任何可以正常工作的东西都会很好。

Thanks.

谢谢。

回答by benvd

Steps I took:

我采取的步骤:

  1. Create a class extending Dialog.
  2. In the onCreate, call setContentView(x, y)with xbeing your R.layout and ybeing 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:
  1. 创建一个扩展Dialog的类。
  2. 在OnCreate,调用setContentView(x, y)x被你R.layout和y被R.style.popupStyle(见下文)。
  3. 在 res/values/style.xml 中,您需要覆盖默认的 DialogWindow 样式。我尝试制作一种以该样式为父样式的样式,但这仍然没有清除所有默认值。所以我检查了Android git tree并获得了默认样式,然后复制粘贴了它。这是一个:
<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

您会遇到一些错误,只需从官方 Android 的 style.xml 和 themes.xml 文件中复制更多内容来解决它们。这是我的styles.xml 文件的内容: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.

感谢 mbaird 让我走上正轨。

[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.xmland themes.xml.

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

回答by Mark B

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.

听起来您正在尝试真正自定义AlertDialog。对于您想要做的事情,您最好创建自己的扩展Dialog的类,类似于通过编写扩展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.

您可以通过在自定义 Dialog 类的 onCreate() 方法中调用 setContentView() 来设置布局 XML,就像在 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.

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