【发布时间】:2019-09-29 11:40:16
【问题描述】:
我一直在使用新的 MaterialButton 类。当用户单击按钮时,我希望按钮上有不同的颜色。
我从一开始就为此目的使用选择器可绘制对象,但它似乎不适用于 MaterialButton。
<com.google.android.material.button.MaterialButton android:layout_width="0dp"
android:text="@string/login"
style="@style/Widget.Mohre.Button"
android:layout_height="wrap_content"
app:cornerRadius="@dimen/card_corner_radius"
android:padding="@dimen/unit_large"
android:id="@+id/loginBtn"
/>
我的 Widget.Mohre.Button 样式
<style name="Widget.Mohre.Button" parent="Widget.MaterialComponents.Button">
<item name="android:textColor">@color/textColorWhite</item>
<item name="android:background">@drawable/mohre_button_selector</item>
<item name="android:textAppearance">@style/TextAppearance.Button</item>
<item name="android:stateListAnimator" tools:ignore="NewApi">@animator/button_state_list_anim</item>
</style>
我的选择器可绘制
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/mohre_button_pressed" android:state_pressed="true" />
<item android:drawable="@drawable/mohre_button_selected" android:state_enabled="false" android:state_pressed="false" />
<item android:drawable="@drawable/mohre_button_normal" />
我的个人可绘制对象只是具有不同颜色的矩形形状
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<corners android:radius="30dp"></corners>
<solid android:color="#3a516a"></solid>
</shape>
按钮根本不采用选择器可绘制的颜色。它只是显示应用程序的默认强调色
【问题讨论】:
-
使用 backgroundTint 代替 android:background
-
你不能将drawabe选择器设置为android:backgroundTInt