gpt4 book ai didi

java - KitKat 上的按钮文本消失(API 级别 19)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:29:27 25 4
gpt4 key购买 nike

我的应用(游戏)的主菜单使用标准的 Android 按钮。它在我的所有设备上运行良好,但Nexus 7 with Android 4.4.2 除外。问题如下:

在以下任一情况下,按钮的文本突然消失:

  • 按钮被按下(当我触摸它时立即发生,无需松开),
  • setEnabled(boolean) 在按钮上调用

例如,如果我按下“加载游戏”,按钮在按下事件期间正确突出显示,但“加载游戏”完全消失(按钮的文本为空)。

如果我删除所有自定义样式和行为,并仅使用默认 Android 按钮和默认字体等,问题仍然存在。

如果我将 targetSdkVersion 降低到 18(从 19),即使在 Nexus 7 上也一切正常。

知道 KitKat 在这方面发生了什么变化吗?我没有发现任何可疑的东西。

我的重要 XML 代码:

<TableLayout
android:id="@+id/layoutGameMainmenu"
android:layout_width="wrap_content"
android:layout_height="83dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="center_vertical"
android:visibility="gone" >

<TableRow>

<Button
android:id="@+id/buttonLoadGame"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="2dip"
android:layout_marginRight="30dip"
android:background="@drawable/button_gamemainmenu"
android:text="buttonLoadGame"
android:textColor="@color/button_gamemainmenu_text"
android:textScaleX="0.9"
android:textSize="16dp" />

<Button
android:id="@+id/buttonSettings"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="2dip"
android:layout_marginLeft="30dip"
android:background="@drawable/button_gamemainmenu"
android:text="buttonSettings"
android:textColor="@color/button_gamemainmenu_text"
android:textScaleX="0.9"
android:textSize="16dp" />
</TableRow>

<TableRow>

<Button
android:id="@+id/buttonStartGame"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="1dip"
android:layout_marginRight="30dip"
android:background="@drawable/button_gamemainmenu"
android:text="buttonStartGame"
android:textColor="@color/button_gamemainmenu_text"
android:textScaleX="0.9"
android:textSize="16dp" />

<Button
android:id="@+id/buttonQuit"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="1dip"
android:layout_marginLeft="30dip"
android:background="@drawable/button_gamemainmenu"
android:text="buttonQuit"
android:textColor="@color/button_gamemainmenu_text"
android:textScaleX="0.9"
android:textSize="16dp" />
</TableRow>
</TableLayout>

关于上述代码的重要说明:

  • 游戏有一个两行的主菜单(每行有两个按钮,所以一共4个按钮),这个主菜单位于屏幕的底部
  • 文字文本只是占位符,因为游戏有自己的文本文件格式,并在创建 Activity 时从那里读取数据
  • 问题仍然存在 即使我完全删除了 android:textColorandroid:background 属性。在这种情况下,我的按钮将具有默认外观(而不是它们的游戏特定样式),但问题仍然存在。
  • 再次强调:以上代码在除 Nexus 7 之外的所有(测试)设备上都能完美运行(除 Nexus 7 外,我的所有设备都是 KitKat 之前的设备)

最后,关于我的全局样式/主题的一些信息:

在 AndroidManifest 中,我将 Application 主题设置为 MyCustomThememycustomtheme.xml内容:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="MyCustomTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:soundEffectsEnabled">false</item>
</style>
</resources>

最后,我的 styles.xml 如下(但似乎我没有从任何地方引用它的样式,这似乎是我们制作全屏游戏时的旧代码,或者Android 默认在任何地方使用它?):

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme" parent="android:Theme">
</style>

<style name="Theme.TranslucentWoTitle" parent="android:Theme.Translucent">
<item name="android:windowNoTitle">true</item>
</style>
</resources>

最佳答案

查看您的 XML 文件,我仍然认为您的项目中存在问题。


这个最小的示例不会在 Nexus 7 2013 4.4.2(minSDK=8targetSDK=19)上重现问题:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

获取您的 XML 并删除背景/文本颜色也不会重现它:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
android:id="@+id/layoutGameMainmenu"
android:layout_width="wrap_content"
android:layout_height="83dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:gravity="center_vertical">

<TableRow>

<Button
android:id="@+id/buttonLoadGame"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="2dip"
android:layout_marginRight="30dip"
android:text="buttonLoadGame"
android:textScaleX="0.9"
android:textSize="16dp" />

<Button
android:id="@+id/buttonSettings"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="2dip"
android:layout_marginLeft="30dip"
android:text="buttonSettings"
android:textScaleX="0.9"
android:textSize="16dp" />
</TableRow>

<TableRow>

<Button
android:id="@+id/buttonStartGame"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="1dip"
android:layout_marginRight="30dip"
android:text="buttonStartGame"
android:textScaleX="0.9"
android:textSize="16dp" />

<Button
android:id="@+id/buttonQuit"
android:layout_width="174dip"
android:layout_height="40dip"
android:layout_marginBottom="1dip"
android:layout_marginLeft="30dip"
android:text="buttonQuit"
android:textScaleX="0.9"
android:textSize="16dp" />
</TableRow>
</TableLayout>

</RelativeLayout>

最后,我注意到 android:textColor="button_gamemainmenu_text",这不是默认的 Android 行为,建议您以自己的方式处理文本颜色。我坚信删除此自定义行为将解决问题。

关于java - KitKat 上的按钮文本消失(API 级别 19),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23588733/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com