我让Button使用自定义drawable来制作圆角。但我想改变不同状态的背景颜色,聚焦,按压等。
我在onCreate中使用了以下内容来更改整体背景颜色。但我也不知道如何定位state_pressed
来改变它的颜色。
有任何帮助吗?
onCreate 中的
按钮 button_rounded.xml 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" />
<?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>
答案 0 :(得分:0)
试试这个,它对我有用:
Drawable drawable = getResources().getDrawable(R.drawable.button_rounded);
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), getResources().getColor(R.color.colorGreen));