How to add title to the custom Dialog?

后端 未结 7 583
猫巷女王i
猫巷女王i 2021-01-12 20:37

How can i add title to this custom dialog??

\"enter

I have tried like this

7条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-12 21:06

    What you might try, if you still need an answer to this, is an AlertDialog.Builder Object. In this Object you can also call the setMessage("Title") method, which will set the title in the Dialog you eventually create with this. Moreover, you can also specify a positiveButton, neutralButton and a negativeButton (let's say "Add", "Ok" and "Cancel" in that order, though you can specify your own text).

    I believe the problem lies in that when you call Dialog dialog = new Dialog(this) the onCreateDialog(int id) is called. But here's the catch: this method is called once and delivers a Dialog which is reused whenever you need a new Dialog. The Dialog however, can't be edited any more (as far as I know). Well maybe with the onPrepareDialog(int id, Dialog dialog) method, but I am still trying to get that working myself. My point is, that after creation, you can't edit the User Interface on the Dialog anymore. So the way that does work is by Overriding the onCreateDialog(int id) in your code, creating an AlertDialog.Builder (or whatever you are basing your Dialog on: ProgressDialog/AlertDialog/etc.) and setting the title, layout and buttons here. After this, you can call the create() method, which will actually build the Dialog with your settings.

    @Override
    public dialog onCreateDialog(int id){
        // Create the View to use in the Dialog.
        LayoutInflater inflater = getLayoutInflater();
        // Inflate the View you want to set as the layout.
        final View layout = inflater.inflate(R.layout.your_dialog_layout,
                                             (ViewGroup) findViewById(R.id.your_parent_view);
        // Create Dialog Builder Object to create Dialog from.
        AlertDialog.Builder adBuilder = new AlertDialog.Builder(this);
        // Set the title to use.
        adBuilder.setMessage("Your title");
        // Add only a positive button.
        adBuilder.setPositiveButton("Add", new DialogInterface.OnClickListener(){
            @Override
            public void onClick(DialogInterface dialog, int which){
                // Handle click on positive button here.
            }
        };
        // Set the layout which you want to use (inflated at the beginning).
        adBuilder.setLayout(layout);
        // After you've set all the options you want to set, call this method.
        AlertDialog dialog = adBuilder.create();
        return dialog;
    }
    

    This will create a Dialog with the title set to "Your Title", which uses the layout you specified, and has one button with the text "Add". Note that the main difference between positive, neutral and negative buttons is that their layout on the Dialog is changed accordingly (positive = left, neutral = middle and negative = right).

    For more information I would advise you to see the documentation about this.

提交回复
热议问题