【问题标题】:Programatically Create Layout to be displayed on an AlertDialog以编程方式创建要在 AlertDialog 上显示的布局
【发布时间】:2015-11-08 16:02:13
【问题描述】:

我想动态创建一个 LinearLayout。

如何设置它以显示在我的 AlertDialog 中?

我见过一些示例,其中布局是通过 XML 创建并膨胀显示的,但是当我可以动态地创建 XML 布局时,我不想创建它。

我仅限于 API 16 = Android 4.1.2

这是我活动中的一个按钮...

public void TestOnClick() {
    Button test_button = (Button) findViewById(R.id.button_test);
    test_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            LinearLayout layout = new LinearLayout(v.getContext());

            //Create a TextView to add to layout
            TextView textview = new TextView(v.getContext());
            textview.setText("My Test");
            layout.addView(textview);

            //Add abunch of other items to the layout
            //blah blah blah

            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
            builder.setView(layout);
            builder.setNeutralButton("Done", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}

【问题讨论】:

  • 请分享一些代码
  • @Roy 在下面查看我的代码
  • @Chairizky - 感谢您的回复,但我想在 AlertDialog 中显示我的自定义布局(以编程方式创建,而不是通过 XML),而不仅仅是在 AlertDialog 上显示一条消息。

标签: android android-linearlayout android-alertdialog android-4.1-jelly-bean


【解决方案1】:

看起来我必须执行以下操作才能以动态和编程方式创建 LinearLayout 并将该布局显示到 AlertDialog 上:

public void TestOnClick() {
    Button test_button = (Button) findViewById(R.id.button_test);
    test_button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            //Create LinearLayout Dynamically
            LinearLayout layout = new LinearLayout(v.getContext());

            //Setup Layout Attributes
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layout.setLayoutParams(params);
            layout.setOrientation(LinearLayout.VERTICAL);

            //Create a TextView to add to layout
            TextView textview = new TextView(v.getContext());
            textview.setText("My Text");

            //Create Spinner
            Spinner spinner = new Spinner(v.getContext());
            String[] string_list = new String[]{"Test 1", "Test 2", "Test 3"};
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_selectable_list_item, string_list);
            spinner.setAdapter(adapter);
            spinner.setGravity(Gravity.CENTER);

            //Create button
            Button button = new Button(v.getContext());
            button.setText("My Button");
            button.setWidth(100);
            button.setHeight(50);

            //Add Views to the layout
            layout.addView(textview);
            layout.addView(spinner);
            layout.addView(button);

            //Create AlertDialog Builder
            AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

            //Give the Dialog a Title
            builder.setTitle("Results");

            //Set the Dynamically created layout as the Dialogs view 
            builder.setView(layout);

            //Add Dialog button that will just close the Dialog
            builder.setNeutralButton("Done", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            });

            //Show the custom AlertDialog
            AlertDialog alert = builder.create();
            alert.show();
        }
    });
}

【讨论】:

    【解决方案2】:

    要将对话框主题应用于活动,只需通过添加 android:theme 属性来修改 AndroidManifest.xml 文件中的元素,如下所示:

     android:theme="@android:style/Theme.Dialog"
    

    这样做会使活动显示为对话框..

    此外,如果您需要隐藏活动标题以不显示..在活动的 OnCreate 方法中添加此行

     requestWindowFeature(Window.FEATURE_NO_TITLE);  
    

    仅此而已,如果这对您有帮助,请点赞... Visit This Page

    【讨论】:

      【解决方案3】:

      你的意思是像这样制作 AlertDialog 吗?

      把它放在你的导入上

      import android.app.AlertDialog;
      import android.content.DialogInterface;
      import android.content.Context;
      

      然后添加此代码

      final Context context = this;   
      
      Button test_button = (Button) findViewById(R.id.button_test);
      test_button.setOnClickListener(new View.OnClickListener() {
      
        @Override
        public void onClick(View arg0) {
      
         AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
          context);
      
         alertDialogBuilder.setTitle("Quit");
      
         alertDialogBuilder
          .setMessage("Are you sure want to Quit?")
          .setCancelable(false)
          .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked, close
            // current activity
           menu.this.finish();
           }
            })
          .setNegativeButton("No",new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog,int id) {
            // if this button is clicked, just close
            // the dialog box and do nothing
            dialog.cancel();
           }
          });
      
          // create alert dialog
          AlertDialog alertDialog = alertDialogBuilder.create();
      
          // show it
          alertDialog.show();
         }
        });
      }   
      

      【讨论】:

      • 感谢您的回复,但这只是使用alerdialog setMessage。我希望能够在 AlertDialog 上显示我的动态自定义布局。
      猜你喜欢
      • 2012-10-01
      • 2019-06-01
      • 1970-01-01
      • 2012-08-07
      • 2018-10-08
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 2020-08-19
      相关资源
      最近更新 更多