簡體   English   中英

使用通用Drawable更改按鈕的顏色狀態

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM