[英]Android: Specifying style of programmatically added radio buttons when focused
我正在開發我的第一個Android應用程序,而且我遇到了一些關於外觀的問題。 我是以編程方式將單選按鈕添加到以線性布局定義的單選按鈕組中。 我使用的是黑暗主題,當布局中定義的任何元素獲得焦點時,它們會以黃色突出顯示:具有焦點的按鈕具有整體黃色高光,而具有焦點的復選框具有黃色文本。 但是對於動態添加的單選按鈕,當單選按鈕獲得焦點時,文本會變為黑色(與背景相同)。
如何指定動態添加的單選按鈕在焦點上使用與其他元素相同的樣式,或者為主題指定自定義焦點樣式?
這是我用來添加單選按鈕的代碼:
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroupVersions);
RadioGroup.LayoutParams rprms;
for(int i=0;i<installedVersionName.size();i++)
{
RadioButton radioButton = new RadioButton(this);
radioButton.setText(installedVersionName.get(i));
radioButton.setId(i);
rprms = new RadioGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
radioGroup.addView(radioButton, rprms);
}
除了為LinearLayout設置android:background="@color/black"
以提供純色背景而不是主題使用的漸變之外,我沒有改變任何外觀。
您可以使用TextView.setTextColor(ColorStateList)來實現此目的。
要創建ColorStateList,只需使用不同的“個案”初始化您感興趣的狀態標志組合。例如:
ColorStateList colors = new ColorStateList(
new int[][] {
new int [] { android.R.attr.state_pressed },
new int [] { android.R.attr.state_selected },
new int[0],
},
new int[] {
highlightedColor,
highlightedColor,
color,
});
tv.setTextColor(colors);
這將在正常狀態下使用“color”,在按下或選擇TextView時使用“highlightedColor”。 有關可能狀態的列表,請參閱R.attr 。 您可能對state_checked感興趣但我沒有測試過這種情況。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.