Android 提供一个以编程方式创建的对话框';s按钮对话框按钮的默认样式

Android 提供一个以编程方式创建的对话框';s按钮对话框按钮的默认样式,android,dialog,android-styles,Android,Dialog,Android Styles,我正在以编程方式创建一个对话框(不是AlertDialog,不要告诉我使用它,因为它会带来另一个无法解决的问题),它有自己的布局层次结构(下面的代码)。因为我使用的是对话框而不是AlertDialog,所以没有“setPositive/NegativeButton”方法,所以我必须像创建对话框的其他视图一样创建按钮。问题是,虽然其他视图使用默认对话框样式,但按钮不使用。我尽我所能,在互联网上和源代码中查找对话框的默认主题和样式,调试程序以找到对话框使用的主题并将其应用于按钮,但没有任何效果。按钮

我正在以编程方式创建一个对话框(不是AlertDialog,不要告诉我使用它,因为它会带来另一个无法解决的问题),它有自己的布局层次结构(下面的代码)。因为我使用的是对话框而不是AlertDialog,所以没有“setPositive/NegativeButton”方法,所以我必须像创建对话框的其他视图一样创建按钮。问题是,虽然其他视图使用默认对话框样式,但按钮不使用。我尽我所能,在互联网上和源代码中查找对话框的默认主题和样式,调试程序以找到对话框使用的主题并将其应用于按钮,但没有任何效果。按钮的样式从来都不是正确的

代码如下:

    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();
dialog=新建对话框(上下文);
RelativeLayout RelativeLayout=新的RelativeLayout(上下文);
relativeLayout.setLayoutParams(新的relativeLayout.LayoutParams(relativeLayout.LayoutParams.MATCH_父级,relativeLayout.LayoutParams.MATCH_父级));
ListView ListView=新的ListView(上下文);
setAdapter(适配器);
relativeLayout.addView(listView);
LinearLayout按钮布局=新的LinearLayout(上下文);
按钮布局。设置方向(线性布局。水平);
RelativeLayout.LayoutParams按钮LayoutParams=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_父项,RelativeLayout.LayoutParams.WRAP_内容);
buttonsLayoutParams.addRule(相对位置对齐\u父项\u底部);
buttonsLayout.setLayoutParams(buttonsLayoutParams);
按钮取消按钮=空;

如果(Build.VERSION.SDK_INT为什么
AlertDialog
会产生无法解决的问题?如果您使用的AlertDialog包含一个列表,其中的项目包含edittext,并且该列表使用适配器,那么当您选择edittext时,键盘将不会显示。我尝试了我找到的所有解决方案,但只有一个使键盘显示出来的解决方案使它出现在屏幕后面。)警报对话框,使其无效。