【问题标题】:App crash when android:theme is aplied to a specific buttonandroid:theme 应用于特定按钮时应用程序崩溃
【发布时间】:2015-09-06 20:30:05
【问题描述】:

我正在为 android(我的第一个应用程序)做一个简单的计算器应用程序,当我尝试将特定的 android:theme 应用于按钮时,我遇到了一些问题。

当具有特定主题的按钮尝试在onclick事件中执行活动方法时,问题就出现了。根据我在 StackOverflow 中搜索的内容,就像具有特定主题的按钮的“上下文”与活动上下文不同,因此它找不到我处理活动中写入的 onclick 的方法。

我在 style.xml 中定义了我的应用程序主题和我的特定按钮主题:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="colorButtonNormal">#dc000000</item>
        <item name="android:background">#dc262626</item>
        <!-- Customize your theme here. -->
    </style>

    <style name="contextButtonTheme" parent="AppTheme">
        <item name="colorButtonNormal">@color/contextButtonsColor</item>
    </style>

</resources>

布局xml中有我的按钮:

<Button
    android:layout_width="0dp"
    android:layout_weight="25"
    android:layout_height="match_parent"
    android:text="X"
    android:id="@+id/multButton"
    android:textSize="11pt"
    android:theme="@style/contextButtonTheme"
    android:onClick="onClickButton"/>

我在这里读到的解决方案是将“android:theme”更改为“style”,虽然这解决了崩溃,但不应用 colorButtonNormal 新颜色:(。

请帮帮我D:

PD:对不起我的英语不好

【问题讨论】:

    标签: android themes


    【解决方案1】:

    我已经回答了一个类似的问题here,您可以在这里获得更多背景知识。

    您的问题的一个可能解决方案是不使用android:onClick="onClickButton",而是在代码中设置onClickListener。这样您就可以保留您的主题Button。官方docs有一个例子如何做到这一点。

    【讨论】:

    • 嗨,我以前看过你的回答并阅读了这两个链接。 onClickListener我没试过,下班回来试试。谢谢你的帮助!
    • 它有效,解决了我的问题!非常感谢男人! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多