如何覆盖Android按钮中按下的材质设计状态
各位博学的朋友好, 我使用的是材质设计主题,但我需要过度估计其状态按下,以便在单击按钮时更改颜色(高亮显示片刻),如下所示 为此,我在按钮上设置了一个可拉伸的,如下所示如何覆盖Android按钮中按下的材质设计状态,android,android-button,android-theme,material-components-android,Android,Android Button,Android Theme,Material Components Android,各位博学的朋友好, 我使用的是材质设计主题,但我需要过度估计其状态按下,以便在单击按钮时更改颜色(高亮显示片刻),如下所示 为此,我在按钮上设置了一个可拉伸的,如下所示 <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="false"> <shape>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="false">
<shape>
<solid android:color="@color/primaryColor" />
<stroke android:color="@color/primaryDarkColor" android:width="@dimen/stroke_width"/>
<corners android:radius="@dimen/corner_radius" />
</shape>
</item>
<item android:state_pressed="true">
<shape>
<solid android:color="@color/secondaryColor" />
<stroke android:color="@color/secondaryLightColor" android:width="@dimen/stroke_width" />
<corners android:radius="@dimen/corner_radius" />
</shape>
</item>
我还得到了一个ThemeOverlay来覆盖按下的状态
<style name="ThemeOverlay.Red.UnitedStates" parent="">
<item name="android:colorPressedHighlight">@color/secondaryColor</item>
</style>
@颜色/第二颜色
不幸的是,当我点击按钮时,突出显示没有发生
我会错过什么
这是材料主题XML
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/primaryColor</item>
<item name="colorPrimaryVariant">@color/primaryLightColor</item>
<item name="colorPrimaryDark">@color/primaryDarkColor</item>
@颜色/原色
@颜色/主灯光颜色
@颜色/原色
您可以只使用app:backgroundTint
属性:
<com.google.android.material.button.MaterialButton
app:backgroundTint="@color/custom_button_selector"
../>
与:
正常状态:
按下状态:
您是如何将可拉伸按钮应用于按钮的?作为背景画?另外,您如何将主题Overlay附加到应用程序主题?注意:您的选择器代码似乎不正确,默认状态(未按下)应始终位于底部。因此,您应该将state_pressed置于默认值之前。检查此链接:嗨@Gabriele,您的解决方案是完美的,非常感谢。
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@color/red600" android:state_pressed="true"/>
<item android:color="?attr/colorPrimary" android:state_enabled="true"/>
<item android:alpha="0.12" android:color="?attr/colorOnSurface"/>
</selector>