更改按钮的启用颜色,而不更改禁用的颜色

时间:2018-05-04 17:40:08

标签: android android-view android-button android-theme android-styles

所以我研究了很多,并且有很多关于按钮和颜色的问题和答案。 但在你投票之前看到这个问题更具体,更实际,我找不到答案。

我使用元素 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当我的按钮被禁用时,会变成灰色。

我不想做的事情:

创建一个新元素并包含布局,我避免这样做,因为我想保留所选动画,提升,填充,提升等。再次创建所有内容并不聪明。

我已尝试过:

更改背景 =它松开了内部衬垫,是什么让按钮更大,我想保持材料设计&#34;规则&#34; enter image description here enter image description here

更改主题 =我尝试通过编辑器和代码更改主题,但发生了两件事:或者我更改了不是按钮的更多内容,或者我更改了启用和禁用颜色。

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

2 个答案:

答案 0 :(得分:6)

您需要更改$editStuState = StuAtt::where('studentId' , '=' , $id)->first(); $editStuState -> leave +=1; $editStuState -> present = $editStuState -> present-1; $editStuState->save(); return 'this is good'; 值,特别是android:colorAccent。这可以通过将主题应用于Button

来实现

内部Button引入了以下更改:

styles.xml

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

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

注意,按钮已应用:

  • <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"

然后你会得到以下输出:

enter image description here

答案 1 :(得分: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>
相关问题