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

9
我的应用程序(一个游戏)的主菜单使用标准的Android按钮。在除了运行着Android 4.4.2的Nexus 7设备之外的所有设备上,它都可以正常工作。问题如下:
无论以下哪种情况发生,按钮的文本会突然消失:
- 按钮被按下时(当我触摸它时,它立即发生,无需释放) - 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以外的所有(经过测试的)设备上都可以完美运行(我的所有设备都是预装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和您的代码。另外,按钮上的文本是空的还是不可见的?尝试从按钮调用getText()并查看文本是否为空。 - Alexander Mikhaylov
我曾经遇到过类似的问题。TextView在没有设置文本时,测量其高度时会出现错误,并且在设置文本后无法正确重新测量。尝试在XML中设置一个占位文本,例如android:text="test",看看问题是否仍然存在。 - Yaroslav Mytkalyk
4
你能否在一个新的项目中重现它?如果可以,请发布那个最小化的代码。如果不能,你的项目中可能有其他问题。对我来说,开始一个新的 Hello World 项目,并将默认的“TextView”更改为“Button”,在 Nexus 7 2013 4.4.2 上不会重现它。 - nhaarman
发你的代码吧,伙计..!! - Pankaj Arora
你的按钮是什么背景颜色?使用什么作为前景颜色? - Leandros
显示剩余4条评论
4个回答

6

通过查看你的XML文件,我仍然认为你的项目出了一些问题。


这个简单的示例在Nexus 7 2013 4.4.2 (minSDK=8, targetSDK=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默认行为,这表明你正在以自己的方式处理文本颜色。我强烈建议删除这种自定义行为,这样可以解决问题。


顺便说一下,我希望能得到一些提示,告诉我该从项目中删除哪些部分作为测试。正如我所说,这个项目非常庞大,我不能轻易地删除它的部分(这是我们和5个朋友自2013年12月以来开发的一个业余游戏)。 - Thomas Calc
删除它 :) 看看会发生什么。我假设你正在使用像Git这样的版本控制,所以不会有任何损失。 - nhaarman
顺便问一下,您能否澄清禁用字符串属性值是什么意思?您是指android:text部分吗? - Thomas Calc
你为按钮添加了任何选择器吗? - Sivakumar
有,但是如果我删除选择器(完全删除对它们的引用),错误仍然存在。 - Thomas Calc
显示剩余13条评论

3
也许您可以使用Eclipse中的“层次结构视图”来调试布局,以获得一些新的见解?它非常有用,因为您可以在运行时读取测量布局参数。至少它应该引导您走上正确的轨道。

谢谢,我会记住的。(希望我能尽快抽出时间来尝试一下。) - Thomas Calc

2

我曾经遇到过相同的问题。在 Nexus 7 和 Nexus 10 上,文本会消失。

出现错误的情景如下:

  1. Use the following layout.
  2. Set text to the textView on the click of the button.

    <TableLayout
        android:id="@+id/tableLayout1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" >
    
        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >
    
            <Button
                android:id="@+id/button1"
                android:layout_width="100dp"
                android:layout_height="wrap_content"
                android:text="Record"
                android:textColor="@android:color/black" />
        </TableRow>
    </TableLayout>
    
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="TextView" />
    

为了解决这个问题,你可以选择从TableLayout转换为LinearLayout或为你的按钮设置宽度。对我来说,这是可行的解决方案。

1

你好,我已经使用你的自定义样式进行了测试。由于无法使用你所用的文本颜色,我使用了硬编码颜色。它在所有设备上都可以正常工作。但我无法在Nexus 7上进行测试,因为我没有这个设备。希望在Nexus 7上也能正常工作。请检查更新后的文件并告诉我反馈。如果你使用了任何选择器,请发布选择器文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:orientation="vertical" >

<TableLayout
    android:id="@+id/layoutGameMainmenu"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:gravity="center_vertical"
    android:orientation="vertical"
    android:padding="5dp"
    android:visibility="visible" >

    <TableRow>

        <Button
            android:id="@+id/buttonLoadGame"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="2dip"
            android:layout_marginRight="30dip"
            android:text="buttonLoadGame"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonSettings"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="2dip"
            android:layout_marginLeft="30dip"
            android:text="buttonSettings"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />
    </TableRow>

    <TableRow>

        <Button
            android:id="@+id/buttonStartGame"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="1dip"
            android:layout_marginRight="30dip"
            android:text="buttonStartGame"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />

        <Button
            android:id="@+id/buttonQuit"
            android:layout_width="wrap_content"
            android:layout_height="40dip"
            android:layout_marginBottom="1dip"
            android:layout_marginLeft="30dip"
            android:text="buttonQuit"
            android:textColor="#000000"
            android:textScaleX="0.9"
            android:textSize="16sp" />
    </TableRow>
</TableLayout>


感谢您的努力。我会尽快回到这个问题上。我使用选择器,但即使我完全禁用(删除)它们,问题仍然存在。 - Thomas Calc

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接