不改变禁用状态颜色的情况下更改按钮启用状态的颜色

7
我将使用Button元素来布局我的xml。 android.widget中的Button类继承自TextView
这个视图可以通过Java代码标记为启用或禁用,使用.setEnabled(true|false)
按钮的xml代码。
<Button
    android:id="@+id/maps_list_save_button"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/str_save"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent" />

我想要做什么:

enter image description here 当我的按钮启用时,我想要给它一个紫色。

enter image description here 当我的按钮禁用时,我想要变成灰色。

我不想做的事情:

创建一个新元素并包含布局,我避免这样做是因为我想保留所选动画、提高、填充、海拔等。重新创建所有内容并不明智。

我已经尝试过的:

更改背景 = 它会失去内部填充,使按钮变大,我想保持材料设计的“规则” enter image description hereenter image description here

更改主题 = 我尝试通过编辑器和代码来更改主题,但出现了两个问题:要么我更改了不是按钮的其他东西,要么我更改了相同颜色的启用和禁用。

即使查找文档,我也没有找到如何正确使用此元素。


1
你尝试使用 selector 了吗? - Md Sufi Khan
@MdSufiKhan 在哪里以及如何? - Canato
2个回答

12

您需要做的是更改android:colorAccent属性值以针对特定的Button。这可以通过将主题应用于Button来实现。

styles.xml文件中进行以下更改:

<style name="PurpleTheme" parent="AppTheme">
    <item name="android:colorAccent">#8654e2</item>
</style>

然后在xml文件中声明以下两个按钮,其中第一个按钮处于启用状态,第二个按钮处于禁用状态。

<Button
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button 1"
    android:theme="@style/PurpleTheme" />

<Button
    style="@style/Widget.AppCompat.Button.Colored"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:enabled="false"
    android:text="Button 2"
    android:theme="@style/PurpleTheme" />

请注意,以下两个属性要应用于按钮:

  • style="@style/Widget.AppCompat.Button.Colored"
  • android:theme="@style/PurpleThemeOverlay"

这样你会得到以下输出:

enter image description here


只使用style="@style/Widget.AppCompat.Button.Colored"已经可以工作了,谢谢!我现在还不能设置赏金,但是我会的。 - Canato
1
@Canato,这意味着您已将colorAccent应用于活动的主题上。如果您想要将活动与不同的accentColor进行主题设置,并使按钮具有另一种颜色,则这是正确的方法。同时,如果有可能其他人提供更好的答案,请随时不要授予悬赏金。只要这个答案被接受了,它就会自动获得奖励。谢谢。 - azizbekian
@Canato,稍微修改了答案。不需要使用 ThemeOverlay - azizbekian
我的IDE告诉我需要API级别21才能使用android:colorAccent。有没有低版本的解决方法? - tzg
@tzg,如果你使用appcompat主题,应该是<item name="colorAccent">#8654e2</item> - azizbekian
2
在我的情况下,我使用了与上面相同的方法,但它可以与 android:colorPrimarycolorPrimary 一起使用。 - Victor Gabriel

3
您可以使用以下选择器来实现此目的。
 <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:state_enabled="true">
    <shape
        android:shape="rectangle">
      <solid android:color="Your Color"></solid>
      <corners android:radius="5dp"></corners>
      <padding android:bottom="2dp" android:left="2dp"
          android:right="2dp" android:top="2dp"></padding>
    </shape>
  </item>
  <item android:state_enabled="false" android:drawable="@color/colorAccent"/>

</selector>

我该如何将这个放在按钮元素中? - Canato
@Canato 使用Android:background="@drawable/selector.xml" - Raja Jawahar
我在讨论一个问题,如果我改变背景,我会失去内部填充等。您可以在“What I already did > Change Background”中看到 =) - Canato
请在问题下面发帖。感谢您提前的帮助。 - Canato
@Canato 我理解你的问题。我已经更新了我的答案,请用这个检查一下。这是因为按钮有一个默认的背景和内在的左右填充,由于我们正在改变背景,所以会发生这种情况。希望我的答案能对问题有所帮助。 - Raja Jawahar
我尝试过,但一直在破坏填充。state_enable工作正常,但raise、pressed也被破坏了 =/ - Canato

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