奇怪的布局问题。 (请参阅附图)

发布于 2024-09-25 13:28:42 字数 1641 浏览 3 评论 0原文

alt text

上面的图片是我的游戏的第一页。通常,大蓝色“按钮”背景图像不应显示在那里。

我在布局文件中为按钮声明了自定义背景图像,如下所示:

<Button android:id="@+id/id_button_startgame"
                             android:layout_width="wrap_content"
                             android:layout_height="48dp"
                             android:layout_weight="1"
                             android:textSize="26sp"
                             android:text="@string/start_game"
                             android:textColor="@color/solid_red"
                             android:background="@drawable/button_bg"

                             />

这是可绘制文件夹中的button_bg.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/button01_focused" /> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button01_pressed" /> 
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button01_pressed" /> 
    <item android:drawable="@drawable/button01_idle" /> 
</selector>

正如您所看到的附加图像。下面的三个按钮是正常情况。我在我的应用程序中随处使用它们。但有时屏幕中央的那个会出现。这个大按钮不能按或任何东西。它就在我的游戏上方盘旋。我可以开始游戏,但显然看不到内容,因为这个蓝色的大东西覆盖了所有内容。

我可以通过多次启动/退出游戏来重现此错误。

我很乐意提供更多信息。但不知道我应该给你什么信息。我使用 FrameLayout 将 UI 放置在 SurfaceView 之上。

大多数时候这个错误不会发生。在我启动/退出游戏并重复几次后,它似乎是随机发生的。

有人以前见过这样的事情吗?如果是这样,请告诉我你是如何解决的?

alt text

The image above is the first page of my game. Normally the big blue 'button' background image should not show up there.

I declare my custom background image for my buttons in layout file like this:

<Button android:id="@+id/id_button_startgame"
                             android:layout_width="wrap_content"
                             android:layout_height="48dp"
                             android:layout_weight="1"
                             android:textSize="26sp"
                             android:text="@string/start_game"
                             android:textColor="@color/solid_red"
                             android:background="@drawable/button_bg"

                             />

And here's button_bg.xml in drawable folder

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/button01_focused" /> 
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/button01_pressed" /> 
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/button01_pressed" /> 
    <item android:drawable="@drawable/button01_idle" /> 
</selector>

As you can see the attached image. The three button below is the normal case. I use them everywhere in my apps. But sometimes the one on the center of the screen shows up. This big button cannot be pressed or anything. It's just up there hovering over my game. I can start the game but obviously can't see the content because this big blue thing cover it all.

I can reproduce this bug by keep launching/exiting my game several times.

I'd love to include more info. But don't know what info should I be giving you. I use FrameLayout to lay the UI above a SurfaceView.

And most of the time this bug DON'T happen. It seems to happen randomly after I launch/exit my game and repeat for several times.

Anyone have seen something like this before? If so please let me know how did you manage to fix it?

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

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

发布评论

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

评论(2

做个ˇ局外人 2024-10-02 13:28:42

问题解决了。谢谢大家。

解决方案:“不要在布局上使用透明背景”

您的答案结合起来帮助我解决了这个问题。

好的,首先。我找到了一种 100% 重现该错误的方法。

我有另一个活动,一个“暂停游戏”活动,它具有半透明背景,启动时将悬停在所有内容上。

重现该错误:
1. 启动“暂停”活动。
2. 滑开键盘。
3. 此错误将出现在“暂停”活动后面。

@Orsol - 是的,我对包含这些按钮的布局使用透明背景。

<LinearLayout android:id="@+id/bottom_ui_container"
                android:orientation="vertical"
                android:background="@drawable/transparent_background"
                android:gravity="bottom"
                 android:layout_width="match_parent"
                android:layout_height="match_parent">

                    <LinearLayout android:id="@+id/title_button_container"
                        android:orientation="horizontal"
                         android:background="@drawable/transparent_background"
                        android:gravity="center"
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content"
                         android:visibility="gone">

这2个布局是让3个按钮在底部水平对齐,就像SS中一样。仍然不明白为什么我滑开键盘后我的“按钮背景”变成了布局的背景...

@mbanzon 和 Cristian - 感谢您的提示! =) 下次遇到问题时我很乐意使用 HierarchyViewer!顺便说一句,我用相机拍摄了这张照片,因为当时我不知道何时/如何重现该错误。

@Octavian Damiean 和 CaseyB - 感谢您的帮助! =)我现在不会发布我的布局,因为它已经解决了。

再次感谢大家!没有你我永远无法解决这个问题!

Problem solved. Thank you everyone.

Solution: "Don't use transparent background on the layout"

Your answers combined helped me solve this.

Ok, first. I found a way to 100% reproduce the bug.

I have another activity, a 'Pause Game' activity which has translucent background and will hover over everything when launched.

To reproduce the bug:
1. Launch the 'Pause' activity.
2. Slide open the keyboard.
3. This bug will appear behind the 'Pause' activity.

@Orsol - Yes I use transparent background for the layout that contains these buttons

<LinearLayout android:id="@+id/bottom_ui_container"
                android:orientation="vertical"
                android:background="@drawable/transparent_background"
                android:gravity="bottom"
                 android:layout_width="match_parent"
                android:layout_height="match_parent">

                    <LinearLayout android:id="@+id/title_button_container"
                        android:orientation="horizontal"
                         android:background="@drawable/transparent_background"
                        android:gravity="center"
                         android:layout_width="match_parent"
                         android:layout_height="wrap_content"
                         android:visibility="gone">

This 2 layout is to have the 3 buttons align horizontally at the bottom like in SS. Still don't understand why my 'button background' become the layout's background after I slide open the keyboard...

@mbanzon and Cristian - Thanks for your tips! =) I'd love to play with hierarchyviewer next time I have problems! BTW.I took this shots using camera because back then I don't know when/how to reproduce the bug.

@Octavian Damiean and CaseyB - Thanks for your help! =) I won't post my layout now since it's solved.

Thank you again everyone! Without you I'd never fix this!

绾颜 2024-10-02 13:28:42

我有同样的问题。在我的情况下,屏幕背景设置为

 <color name="transparent">#00000000</color>

问题已通过将其替换为解决

<color name="transparent">@android:color/transparent</color>

I had the same issue. In my case screen background was set to

 <color name="transparent">#00000000</color>

issue was solved by replacing it to

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