![](/img/trans.png)
[英]How to set the background of button programmatically when that is at some of conditions in android?
[英]android set button background programmatically
我想知道如何以編程方式設置按鈕顏色? 我編寫了以下代碼但失敗了:
Button11.setBackgroundColor(R.color.red);
謝謝!!
R.color.red
是一個 ID(也是一個int
),但不是顏色。
請改用以下其中一項:
// If you're in an activity:
Button11.setBackgroundColor(getResources().getColor(R.color.red));
// OR, if you're not:
Button11.setBackgroundColor(Button11.getContext().getResources().getColor(R.color.red));
或者,或者:
Button11.setBackgroundColor(Color.RED); // From android.graphics.Color
或者,獲得更多專業技能:
Button11.setBackgroundColor(0xFFFF0000); // 0xAARRGGBB
舊線程,但學到了一些新東西,希望這可能對某人有所幫助。
如果您想更改背景顏色但保留其他樣式,那么以下內容可能會有所幫助。
button.getBackground().setColorFilter(ContextCompat.getColor(this, R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
您在2020 年及以后尋找的答案:
setColorFilter(color, mode)
自 API 29 以來已棄用(如此處討論)button.setBackgroundColor(color)
弄亂了按鈕樣式現在設置按鈕顏色的正確方法是使用BlendModeColorFilter()
( 參見文檔)。
用法:
btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY)
如果您也使用較舊的 API:
fun setButtonColor(btn: Button, color: Int) {
if (Build.VERSION.SDK_INT >= 29)
btn.background.colorFilter = BlendModeColorFilter(color, BlendMode.MULTIPLY)
else
btn.background.setColorFilter(color, PorterDuff.Mode.MULTIPLY)
}
請投票幫助其他人找到這個答案——我花了很長時間才弄明白 ^^
您可以通過編程方式將所需的顏色設置為按鈕,例如:
Button11.setBackgroundColor(Android.Graphics.Color.parseColor("#738b28"));
您還可以為按鈕提供文本顏色,例如:
Button11.setTextColor(Android.Graphics.Color.parseColor("#FFFFFF"));
我發現 Android Studio 警告我在嘗試執行此操作時已棄用getColor()
:
Button11.setBackgroundColor(getResources().getColor(R.color.red))
所以我發現執行以下方法是簡單的最新解決方案:
Button11.setBackgroundColor(ContextCompat.getColor(context, R.color.red))
您希望避免在顏色參數中進行硬編碼,因為它被認為是糟糕的代碼風格。
編輯:將setBackgroundColor()
與我自己的按鈕一起使用后,我看到內部按鈕填充展開了。 我找不到任何方法將其改回高度和寬度都設置為“wrap_content”。 也許它是一個錯誤。
來源: https ://stackoverflow.com/a/32202256/6030520
為了在設置背景顏色時不更改按鈕的大小:
button.getBackground().setColorFilter(button.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
這並沒有改變按鈕的大小,也適用於舊的 android 版本。
使用setBackgroundColor()會影響樣式。 因此,聲明一個與前一個按鈕具有相同屬性的新樣式,但顏色不同。
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/green"/>
<corners android:radius="10dp"/>
</shape>
現在,使用 OnClick 方法。
location.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
location.setBackgroundResource(R.drawable.green);
}
});
這會更改按鈕,但看起來類似於更改背景。
來自@finnmglas,截至 2021 年的 Java 答案是:
if (Build.VERSION.SDK_INT >= 29)
btn.getBackground().setColorFilter(new BlendModeColorFilter(color, BlendMode.MULTIPLY));
else
btn.getBackground().setColorFilter(color, PorterDuff.Mode.MULTIPLY);
button.setBackgroundColor(getResources().getColor(R.color.red);
設置此視圖的背景顏色。 參數:color 背景顏色
R.color.red
是在 gen 中編譯時生成的參考。
您可以使用下面的代碼作為按鈕顏色,而不會影響樣式(如果使用的話)
yourbtnid.background.setColorFilter(ContextCompat.getColor(context, yourcolor), PorterDuff.Mode.SRC)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.