Android FrameLayout 在初始设置为不可见时无法将 View 设置为可见

发布于 2024-12-22 16:21:25 字数 1450 浏览 2 评论 0原文

我在 android FrameLayout 中遇到了一个非常奇怪的问题。

我正在尝试一个非常简单的游戏,它有一个 SurfaceView,当游戏结束时,我想在顶部弹出另一个视图,可能显示一些文本。这与 Android 示例 Lunarlander 代码非常相似。但是我无法让它工作。这是一个奇怪的问题:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/game_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.tmh.FunMatch.MainGamePanel
  android:id="@+id/game_panel"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.tmh.FunMatch.MyText
      android:id="@+id/text"
      android:text="test"
      android:visibility="invisible"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:gravity="center_horizontal"
      android:textColor="#88ffffff"
      android:textSize="24sp"/>
 </RelativeLayout>

对于上面的 FrameLayout,我想在游戏结束时显示第二个 TextView。但是,在我调用 TextView.setVisibility(VISIBLE) 后,它不会显示。

我做了很多实验,如果我最初将 TextView 设置为 VISIBLE,然后在代码中的某个时刻将其设置为 INVISIBLE。 (我的理解是 TextView 必须最初在屏幕上显示一次,然后我将其变为不可见)。如果我这样做,当游戏结束时,TextView 将正确显示。

我非常怀疑这是安卓的bug。我更改了 android LunarLander 代码,我不知道为什么 LunarLander 能够工作并在屏幕上显示文本。

如果有人遇到同样的问题,请告诉我我的代码有什么问题或者是否存在隐藏的错误?

谢谢

I have met a very strange issue in android FrameLayout.

I am trying a very simple game which has a SurfaceView, and when the game ends, I want to popup another view on top probably displaying some text. This is very similar to android sample lunarlander code. However I can't get it work. Here is the strange issue:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/game_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<com.tmh.FunMatch.MainGamePanel
  android:id="@+id/game_panel"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <com.tmh.FunMatch.MyText
      android:id="@+id/text"
      android:text="test"
      android:visibility="invisible"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerInParent="true"
      android:gravity="center_horizontal"
      android:textColor="#88ffffff"
      android:textSize="24sp"/>
 </RelativeLayout>

For above FrameLayout, I want to show the second TextView when game finished. However it won't show after I call TextView.setVisibility(VISIBLE).

I did a lot experiment, if I initially set the TextView as VISIBLE, then at some point set it to INVISIBLE in the code. (My understanding is the TextView must be initially showed on the screen once then I turned it into INVISIBLE). If I did so, when the game finished, the TextView will be correctly shown.

I highly doubt it's an android bug. I changed android LunarLander code, I don't know why LunarLander did work and show the Text on the screen.

If anyone had run into the same issue, please tell me what's wrong with my code or is there a hidden bug?

Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文