[英]Change Colors States of Button using a Generic Drawable
我有使用自定义可绘制绘制圆角的按钮。 但是我想更改不同状态,聚焦,按下等的背景颜色。
我在onCreate中使用了以下内容来更改整体背景色。 但是我也不知道如何以state_pressed
为目标来更改其颜色。
有什么帮助吗?
在onCreate中
DrawableCompat.setTint(btnLogin.getBackground(), ContextCompat.getColor(getApplicationContext(), R.color.colorGreen));
纽扣
<Button
android:id="@+id/btnLogin"
style="?android:textAppearanceSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="@drawable/button_rounded"
android:text="Sign In"
android:textColor="@android:color/white"
android:textStyle="bold" />
button_rounded.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#5c89c1" />
</shape>
</item>
<item android:state_focused="true">
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#204778" />
</shape>
</item>
<item>
<shape android:shape="rectangle">
<corners android:radius="10dip" />
<solid android:color="#204778" />
</shape>
</item>
</selector>
试试这个,它对我有用:
Drawable drawable = getResources().getDrawable(R.drawable.button_rounded); drawable = DrawableCompat.wrap(drawable); DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.colorGreen));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.