[英]Setting button tint on radio button programmatically
我想以編程方式設置單選按鈕色調。 在 xml 中有一個名為“buttonTint”的屬性來完成這項工作。 但在程序中,我找不到任何方法來為單選按鈕設置色調或顏色。 有什么方法或方法可以做到這一點嗎?
<RadioButton
android:buttonTint="@color/colorPrimaryDark"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Payeer" />
您可以使用setButtonTintList (ColorStateList tint)
將色調應用於可繪制的按鈕。 不修改當前的色調模式,默認為 SRC_IN。
對
setButtonDrawable(Drawable)
后續調用將自動改變可繪制對象並使用setTintList(ColorStateList)
應用指定的色調和色調模式。
示例代碼
public class MainActivity extends AppCompatActivity {
RadioButton radioButton;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioButton = findViewById(R.id.radioButton);
ColorStateList myColorStateList = new ColorStateList(
new int[][]{
new int[]{getResources().getColor(R.color.colorPrimaryDark)}
},
new int[]{getResources().getColor(R.color.colorAccent)}
);
radioButton.setButtonTintList(myColorStateList);
}
}
基於先前的答案,用於設置背景顏色的一行代碼是
Java代碼
button.setButtonTintList(ColorStateList.valueOf(getColor(R.color.red)));
科特林代碼
button.buttonTintList=ColorStateList.valueOf(getColor(R.color.red))
使用以下代碼:
button.setBackgroundTintList(ColorStateList.valueOf(resources.getColor(R.id.red)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.