自定义对话框背景表现得很奇怪
我正在为 Android 创建一个小游戏。目前我正在为菜单屏幕创建用户界面。
当我正在做一个木制主题时,我还想使用自定义对话框来显示高分等,以便它遵循主题。
我找到了一些很好的指南,但是我在对话框的背景上遇到了这个非常奇怪的问题。该对话框几乎是透明的。
我所做的: - 创建了一个dialog_theme.xml:使用
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
- 我需要的元素创建了custom_dialog.xml(用于标题和内容的TextView,以及用于关闭的按钮)
- 创建了一个扩展Dialog的CustomDialog类,并让我可以轻松地使用内容和内容构建这些自定义对话框title 我想
- 在活动中使用 CustomDialog 来创建对话框
(我用于此博客的主要指南.androgames.net/10/custom-android-dialog/ )
问题是透明背景并不总是透明的(在后台显示活动用户界面)。我在此菜单中有 4 个自定义按钮。问题是,不是只将对话框显示为透明并在背景中显示整个 ui,而是将按钮的图像之一拉伸并填充整个对话框背景。如果我只为这个按钮使用标准背景,那么对话框背景是透明的,并按应有的方式在背景中显示活动用户界面。
由于我可能不善于解释,我将展示我的意思的图片: - 导致问题的按钮代码:
<Button
android:id="@+id/id_about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/id_achievements_button"
android:layout_marginTop="15dp"
android:background="@drawable/selector_about" />
给出以下结果:(抱歉,但我还不能直接在帖子中使用图片) http://dl.dropbox.com/u/2980431/wrong.png
将按钮代码修改为:
<Button
android:id="@+id/id_about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/id_achievements_button"
android:layout_marginTop="15dp"/>
给出以下结果: http://dl.dropbox.com/u/2980431/ Correct.png
希望有人知道为什么会发生这种情况,以及解决它的解决方案 - 说实话,我完全迷失了。
I am creating a small game for Android. At the moment I'm just creating the UI for the menu screen.
As I'm doing a wooden theme, I also want to use a custom dialog for showing highscores etc so it follows the theme.
I have found some good guides, but I have this very strange problem with the background of the dialog. The dialog is almost transparent.
What I have done:
- created a dialog_theme.xml with:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
</resources>
- created custom_dialog.xml with the elements I need (TextView for title and content, and button to close)
- created a CustomDialog class which extends Dialog, and lets me build these custom dialogs rather easy with the content and title I want
- using the CustomDialog in the activity to create the dialog
(the main guide I used for this blog.androgames.net/10/custom-android-dialog/ )
The problem is that the transparent background isn't always transparent (showing the activity ui in the background). I have 4 custom buttons in this menu. Problem is that instead of just showing the dialog transparent and showing the whole ui in the background, then one of the images for a button is stretched and fills the whole dialog background. If I just use a standard background for this one button then the dialog background is transparent and shows the activity ui in the background as it should.
As I might have been bad at explaining I will show pictures of what I mean:
- Code for the button that causes the problem:
<Button
android:id="@+id/id_about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/id_achievements_button"
android:layout_marginTop="15dp"
android:background="@drawable/selector_about" />
Gives this result: (sorry but I can't use pictures directly in the post yet)
http://dl.dropbox.com/u/2980431/wrong.png
Modifying the button code to:
<Button
android:id="@+id/id_about_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/id_achievements_button"
android:layout_marginTop="15dp"/>
Gives this result:
http://dl.dropbox.com/u/2980431/correct.png
Hope someone got an idea about why this is happening, and a solution to fix it - to be honest I am totally lost.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仍然不确定发生了什么事。在另一个项目中,我遇到了同样的事情 - 自定义半透明对话框背景,将另一个可绘制对象添加到背景中。重命名后台显示的错误可绘制对象,然后清理项目为我解决了这个问题。
奇怪的。
Still not sure what happened. In another project I came across the same thing - custom semi transparent dialog background, got another drawable added to the background. Renaming the wrong drawable showing in the background, and then clean the project fixed this for me.
Strange.