简体   繁体   English

如何使警报对话框中的复选框左对齐?

[英]How to make checkbox in alert dialog align left?

It is possible to modified the checkbox align left in alert dialog? 是否可以修改警报对话框中的左对齐复选框?

this is activity.java file 这是activity.java文件

AlertDialog dialog; 

final CharSequence[] items = {" Easy "," Medium "," Hard "," Very Hard "};
            // arraylist to keep the selected items
            final ArrayList seletedItems=new ArrayList();

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Select The Difficulty Level");
            builder.setMultiChoiceItems(items, null,
                    new DialogInterface.OnMultiChoiceClickListener() {
             // indexSelected contains the index of item (of which checkbox checked)
             @Override
             public void onClick(DialogInterface dialog, int indexSelected,
                     boolean isChecked) {
                 if (isChecked) {
                     // If the user checked the item, add it to the selected items
                     // write your code when user checked the checkbox 
                     seletedItems.add(indexSelected);
                 } else if (seletedItems.contains(indexSelected)) {
                     // Else, if the item is already in the array, remove it 
                     // write your code when user Uchecked the checkbox 
                     seletedItems.remove(Integer.valueOf(indexSelected));
                 }
             }
         })
          // Set the action buttons
         .setPositiveButton("OK", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int id) {
                 //  Your code when user clicked on OK
                 //  You can write the code  to save the selected item here

             }
         })
         .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int id) {
                //  Your code when user clicked on Cancel

             }
         });

            dialog = builder.create();//AlertDialog dialog; create like this outside onClick
            dialog.show();
    }

......................................................................... ................................................... .......................

http://i.stack.imgur.com/rTpYb.jpg"> http://i.stack.imgur.com/rTpYb.jpg“>

but... I want the checkbox displayed at the left side...anyone help me. 但是...我希望复选框显示在左侧...任何人都可以帮助我。

You can put custom view in your alertdialog. 您可以在自定义视图中放入自定义视图。 In that view you can easily align your checkbox where you want. 在该视图中,您可以轻松地将复选框对准所需的位置。 For more read this tutorial. 有关更多信息,请阅读本教程。 http://www.pcsalt.com/android/create-alertdialog-with-custom-layout/#sthash.yN5edpAX.FKWQjxBj.dpbs http://www.pcsalt.com/android/create-alertdialog-with-custom-layout/#sthash.yN5edpAX.FKWQjxBj.dpbs

Try this way,hope this will help you to solve your problem. 尝试这种方式,希望这将帮助您解决问题。

dialog = builder.create();
dialog.getWindow().setGravity(Gravity.LEFT);
dialog.show();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM