[英]Programmatically change individual item background colour in Android AlertDialog
[英]android how to change background colour in AlertDialog
我正在制作一個使用Alertdialog Builder的應用程序,它可以正常工作,但是問題是我無法更改該警報框標題的顏色和取消按鈕的背景。我該怎么做,這是我的代碼
private void SingleChoiceWithRadioButton() {
builder = new AlertDialog.Builder(this,R.style.myCoolDialog);
builder = new AlertDialog.Builder(this);
builder.setTitle("Select Country Code");
builder.setSingleChoiceItems(CountryCodeArray, -1,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int value) {
String country = "";
String[] countries = CountryCodeArray[value].split(" ");
dialog.dismiss();
spinner_text.setText( countries[0]);
System.out.println(""+spinner_text.getText().toString());
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert = builder.create();
alert.show();
}
這是圖像
提前致謝...:)
最終我做到了。創建了一個自定義對話框。
private void showdialog() {
final Dialog dialog = new Dialog(RegisterScreen.this, R.style.CenterDialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_dialog_layout);
dialog.setCancelable(true);
final Spinner sp = (Spinner) dialog.findViewById(R.id.spinner);
ArrayAdapter<String> adapter_chocolate = new ArrayAdapter<String>(RegisterScreen.this,
android.R.layout.simple_spinner_item, CountryCodeArray);
sp.setAdapter(adapter_chocolate);
sp.setOnItemSelectedListener(new MyOnItemSelectedListener());
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(RegisterScreen.this);
//alertDialogBuilder.setTitle("Missing data");
alertDialogBuilder.setCancelable(true); //if you set this false, the user will not be able to cancel the dialog by clicking BACK button
// alertDialogBuilder.setMessage("You haven't set the quantity!");
/*alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialogBuilder.show(); //don't forget to show the dialog! It is a common mistake.
*/
Button btnCancel = (Button) dialog.findViewById(R.id.Button_Cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
dialog.dismiss();
}
});
dialog.show();
}
它的工作... :)
默認情況下,對話框是從應用程序的主題中獲取主題的。因此,要更改顏色,您需要更改主題:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Dialog" parent="android:style/Theme.Dialog">
<item name="android:windowBackground">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
</style>
</resources>
一個詳細的例子在這里。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.