我正在以编程方式创建一个Dialog(不是AlertDialog,并且不要告诉我使用一个,因为它会产生另一个无法解决的问题),它有自己的布局层次结构(下面的代码)。由于我使用的是Dialog而不是AlertDialog,因此没有" setPositive / NegativeButton"方法所以我必须像对待Dialog的其他视图一样创建按钮。问题是,虽然其他视图使用默认对话框样式,但按钮不会。我尽我所能,在互联网和源代码中查看对话框的默认主题和样式,调试程序以找到对话框使用的主题并将其应用于按钮,但没有任何效果。按钮的风格从来都不是正确的。
这里是代码:
dialog = new Dialog(context);
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT));
ListView listView = new ListView(context);
listView.setAdapter(adapter);
relativeLayout.addView(listView);
LinearLayout buttonsLayout = new LinearLayout(context);
buttonsLayout.setOrientation(LinearLayout.HORIZONTAL);
RelativeLayout.LayoutParams buttonsLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
buttonsLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
buttonsLayout.setLayoutParams(buttonsLayoutParams);
Button cancelButton = null;
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
cancelButton = new Button(new ContextThemeWrapper(context, android.R.style.Theme_Dialog));
//this seems to use the right style for buttons on android <= 2.3.3
} else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1 && Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2) {
cancelButton = new Button(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog));
//this is not the right style. see image below
} else {
cancelButton = new Button(context);
cancelButton.setBackgroundColor(android.R.drawable.dialog_holo_light_frame);
//this sets the background color of the buttons to match the one of the dialog,
//but this way the buttons have no visible margins and click animation
}
cancelButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
cancelButton.setText("Cancel");
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
Button sendButton = null;
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
sendButton = new Button(new ContextThemeWrapper(context, android.R.style.Theme_Dialog));
//this seems to use the right style for buttons on android <= 2.3.3
} else if(Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1 && Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2) {
sendButton = new Button(new ContextThemeWrapper(context, android.R.style.Theme_Holo_Light_Dialog));
//this is not the right style. see image below
} else {
sendButton = new Button(context);
sendButton.setBackgroundColor(android.R.drawable.dialog_holo_light_frame);
//this sets the background color of the buttons to match the one of the dialog,
//but this way the buttons have no visible margins and click animation
}
sendButton.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1));
sendButton.setText("Send");
sendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//do something
dialog.dismiss();
}
});
buttonsLayout.addView(cancelButton);
buttonsLayout.addView(sendButton);
relativeLayout.addView(buttonsLayout);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(relativeLayout);
dialog.setTitle("Insert");
dialog.show();
这里是android&gt;中的结果2.3.3&amp;&amp; &lt; = 3.2:
和android&gt;上的结果3.2:
(抱歉用油漆编辑的按钮,ahah)