【问题标题】:What LayoutParams should be used in an AlertDialog?在 AlertDialog 中应该使用哪些 LayoutParams?
【发布时间】:2015-10-16 12:58:02
【问题描述】:

我有一个EditText 对象:

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

我想使用构建器将此视图添加到AlertDialog

    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) {

                }
            });

我知道有一个方法setView(int)但是我使用的是API 15所以我只能使用setView(View),这也是我通过代码创建视图的原因。

到目前为止一切顺利,但我现在需要设置视图的LayoutParams。我应该选择什么LayoutParams?我知道在RelativeLayout 中你使用RelativeLayout.LayoutParams 而在LinearLayout 中你使用LinearLayout.LayoutParams。但是我应该在对话框中使用什么?

【问题讨论】:

  • 你不需要以编程方式创建布局来使用你知道的 setView 吗?

标签: java android android-layout dialog


【解决方案1】:

根据源代码here,alertdialog的根元素是LinearLayout,所以我建议使用LinearLayout.LayoutParams

【讨论】:

  • 它现在使用FrameLayout.LayoutParams
【解决方案2】:

即使您想将setViewview 作为参数,您也始终可以在xml 中创建视图并使用布局充气器对其进行充气,然后将其用作参数。

示例:

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

【讨论】:

    【解决方案3】:

    这取决于您是否为自定义视图充气。

    • 如果你这样做,你应该使用 LayoutParams 根据 自定义视图容器(线性或相对)。
    • 如果您不使用自定义视图,您 可以设置线性布局参数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2010-11-26
      • 2021-11-20
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多