我試圖在Android應用程序中提供特定顏色的快速選擇,並且希望能夠在簡單對話框中將列表選項設置爲特定顏色。似乎應該很容易,但我一直無法找到直接的答案。以編程方式更改Android AlertDialog中的單個項目背景顏色
我該如何(以編程方式在運行時)相應地設置每個選項的背景顏色?
我試圖在Android應用程序中提供特定顏色的快速選擇,並且希望能夠在簡單對話框中將列表選項設置爲特定顏色。似乎應該很容易,但我一直無法找到直接的答案。以編程方式更改Android AlertDialog中的單個項目背景顏色
我該如何(以編程方式在運行時)相應地設置每個選項的背景顏色?
感謝您的回覆,夥計們。我已經想通了......
private void showColourChooserDialog(){
final ColourItem[] items = {
new ColourItem(Color.RED),
new ColourItem(Color.GREEN),
new ColourItem(Color.BLUE),
};
ListAdapter adapter = new ArrayAdapter<ColourItem>(
this,
android.R.layout.select_dialog_item,
android.R.id.text1,
items){
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
TextView tv = (TextView)v.findViewById(android.R.id.text1);
int colour = items[position].getColour();
tv.setBackgroundColor(colour);
return v;
}
};
new AlertDialog.Builder(this)
.setTitle("Choose Colour")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// ... respond to choice here
}
}).show();
}
的ColourItem類:
public class ColourItem {
private String displayString;
private int colour;
public ColourItem (int colour) {
this(colour, "");
}
public ColourItem (int colour, String displayString) {
this.colour = colour;
this.displayString = displayString;
}
@Override
public String toString() {
return displayString;
}
public void setDisplayString(String s){
this.displayString = s;
}
public int getColour(){
return colour;
}
public void setColour(int i){
this.colour = i;
}
}
答案的靈感來自於:This solution regarding custom icons for dialog items
我想這可能是有用的:
private void showAlertDialogForColors() {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View alertLayout = inflater.inflate(R.layout.choose_color_theme, null);
//alert.setTitle("Theme Color");
// this is set the view from XML inside AlertDialog
alert.setView(alertLayout);
final Integer tempValue = SessionManager.getInstance(ColorSettingsActivity.this).getPrefThemeCode();
TextView textViewBlue = (TextView) alertLayout.findViewById(R.id.themeColorBlue);
TextView textViewOrange = (TextView) alertLayout.findViewById(R.id.themeColorOrange);
TextView textViewPink = (TextView) alertLayout.findViewById(R.id.themeColorPink);
TextView textViewPurple = (TextView) alertLayout.findViewById(R.id.themeColorPurple);
TextView textViewCyan = (TextView) alertLayout.findViewById(R.id.themeColorCyan);
TextView textViewDarkGreen = (TextView) alertLayout.findViewById(R.id.themeColorDarkGreen);
selectedThemeColor = (TextView) alertLayout.findViewById(R.id.textViewSelectedTheme);
selectedThemeColor.setTypeface(font);
textViewBlue.setOnClickListener(this);
textViewOrange.setOnClickListener(this);
textViewPink.setOnClickListener(this);
textViewPurple.setOnClickListener(this);
textViewCyan.setOnClickListener(this);
textViewDarkGreen.setOnClickListener(this);
// disallow cancel of AlertDialog on click of back button and outside touch
alert.setCancelable(false);
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
SessionManager.getInstance(MessageSettingsActivity.this).setPrefThemeCode(tempValue);
Toast.makeText(getBaseContext(), "Cancel clicked", Toast.LENGTH_SHORT).show();
}
});
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
recreate();
}
});
AlertDialog dialog = alert.create();
dialog.show();
}
如果你有一組預定義的顏色使用,或者您想通過動態存儲在Shared Preferences
或DB
顏色和填充列表上
然後在我的情況下,當他點擊特定項目時,執行切換以執行您的操作,其中有5個常量。
後至今你已經嘗試 – ik024
代碼你必須建立自定義對話框請參閱@Ramesh文章 –