android - 以编程方式创建要显示在 AlertDialog 上的布局

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

我想动态创建一个LinearLayout。

如何将其设置为在我的 AlertDialog 中显示?

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

我仅限于 API 16 = Android 4.1.2

这是我的 Activity 上的一个按钮...

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();
        }
    });
}

最佳答案

看起来我必须执行以下操作才能动态地以编程方式创建 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();
        }
    });
}

关于android - 以编程方式创建要显示在 AlertDialog 上的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32026299/

相关文章:

android - 将一个 View 置于另一个 View 之上

android - 如何修复单击时错误的 alertDialog?

java - Google Play 商店接受自签名证书应用吗?

java - SimpleDateFormat 在 android 5.1.1 中不起作用吗?

android - 有机会一起使用 holoeverywhere 和 appcompat 吗?

android - 在layout中动态添加scrollview,在scrollview中添加linearlayout

Android NFC 在启动应用程序时传递单个参数

android - 如何在线性布局周围创建边框,如下图所示?

android - 如何在 Android 上重复使用 AlertDialog 表示是/否?

java - 如果对话框文本字段与私有(private)文本字段匹配,系统退出