【问题标题】:dynamically changing button theme动态改变按钮主题
【发布时间】:2018-07-01 17:05:25
【问题描述】:

我的按钮有两种样式:

<style name="PrimaryButton1" parent="Theme.AppCompat">
    <item name="colorButtonNormal">@color/white</item>
    <item name="colorControlHighlight">@color/blackt</item>
</style>
<style name="PrimaryButton2" parent="Theme.AppCompat">
    <item name="colorButtonNormal">@color/black</item>
    <item name="colorControlHighlight">@color/white</item>
</style>

我知道我可以像这样在布局 xml 中设置主题:
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:theme="@style/PrimaryButton1"/>

但我想在点击事件发生时更改这两种样式之间的主题。
(我使用主题的原因是更改按钮的背景使其显示效果删除)
我怎样才能做到这一点?

【问题讨论】:

    标签: java android android-button


    【解决方案1】:
    1. 为按钮添加一个idandroid:onClick="{method name}"添加到按钮XML
    2. 将此代码添加到您的 Activity / Fragment
      (假设您已为名为 button 的 Button 定义了一个变量)

      内部

      活动: button.setTextAppearance(getApplicationContext(), R.style.PrimaryButton2);
      片段:button.setTextAppearance(getContext(), R.style.PrimaryButton2);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2018-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多