What LayoutParams should be used in an AlertDialog?

早过忘川 提交于 2019-12-11 10:24:53

问题


I have a EditText object:

    EditText textbox = new EditText (this);
    textbox.setHint (something);

I want to add this view to an AlertDialog using the builder.

    AlertDialog.Builder builder = new AlertDialog.Builder (this);
    builder.setTitle (R.string.enter_password_name)
            .setPositiveButton (R.string.save_text, new DialogInterface.OnClickListener () {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton (R.string.cancel_text, new DialogInterface.OnClickListener () {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            });

I know that there is a method setView(int) but I am using API 15 so I can only use setView(View), which is also a reason why I am creating the view by code.

So far so good, but I now need to set the LayoutParams of the view. What LayoutParams should I choose? I know that in a RelativeLayout you use a RelativeLayout.LayoutParams and in a LinearLayout you use a LinearLayout.LayoutParams. But what should I use in a dialog?


回答1:


According to the source code here , alertdialog's root element is a LinearLayout, so I would suggest using LinearLayout.LayoutParams




回答2:


Even if you want to setView with view as a parameter you can always create your view in xml and inflate it using a layout inflator and then use it as a parameter.

example:

View view = getLayoutInflater().inflate(R.layout.yourlayout, null);
setView(view);



回答3:


It depends if you inflate a custom view or not.

  • If you do that, you should use the LayoutParams according the custom view container (Linear or relative).
  • If you don't use a custom view you can set Linear LayoutParams.


来源:https://stackoverflow.com/questions/33171114/what-layoutparams-should-be-used-in-an-alertdialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!