如何在自定义对话框中修复对齐?

时间:2014-09-03 10:13:20

标签: android dialog customdialog

我想要做的是在android中的自定义对话框,这里是图片:

enter image description here

正如你可以看到高度与它的编辑文本之间有一个很大的空间,如何将第二行中的所有组件都放到左侧或水平居中,这是我的代码:

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FullImageActivity2.this);
                    final TextView title = new TextView(FullImageActivity2.this);
                    title.setTextColor(Color.parseColor("#000000"));
                    title.setText("Add To Cart");
                    title.setPadding(5, 10, 0, 10);

                    alertDialogBuilder.setCustomTitle(title);

                    LinearLayout ll=new LinearLayout(FullImageActivity2.this);
                     final LayoutParams lparams = new LayoutParams(50,50);
                     lparams.weight=(float) 0.25;
                     lparams.leftMargin=3;
                     lparams.rightMargin=3;
                     ll.setOrientation(LinearLayout.VERTICAL);
                    TableLayout table = new TableLayout (FullImageActivity2.this);
                    TableRow row0= new TableRow(FullImageActivity2.this);
                    TableRow row1= new TableRow(FullImageActivity2.this);


                    TableRow.LayoutParams tr1 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT);
                    tr1.gravity=1;

                    //tr1.topMargin=15;
                    //tr1.bottomMargin=10;
                    row1.setLayoutParams(tr1);
                    row0.setLayoutParams(tr1);

                    final EditText height = new EditText(FullImageActivity2.this);
                    final TextView heightTxt = new TextView(FullImageActivity2.this);

                    final EditText width = new EditText(FullImageActivity2.this);
                    final TextView widthTxt = new TextView(FullImageActivity2.this);

                    final TextView hint = new TextView(FullImageActivity2.this);

                    height.setInputType(InputType.TYPE_CLASS_NUMBER);

                    height.setLayoutParams(lparams);

                    width.setInputType(InputType.TYPE_CLASS_NUMBER);
                    //height.setPadding(3, 0, 3, 0);
                    width.setLayoutParams(lparams);

                    heightTxt.setText("height");
                    heightTxt.setTextColor(Color.parseColor("#000000"));
                    heightTxt.setPadding(7, 0, 5, 0);

                    hint.setText("please enter height and width in meter");
                    hint.setTextColor(Color.parseColor("#000000"));
                    hint.setPadding(5, 0, 5, 0);

                    widthTxt.setText("width");
                    widthTxt.setTextColor(Color.parseColor("#000000"));
                    widthTxt.setPadding(5, 0, 5, 0);

                    row0.addView(hint);
                    row1.addView(heightTxt);
                    row1.addView(height);
                    row1.addView(widthTxt);
                    row1.addView(width);
                    table.addView(row0);
                    table.addView(row1);



                   // ll.addView(height);
                    //ll.addView(heightTxt);
                    ll.addView(table);


                    alertDialogBuilder.setView(ll);

                    alertDialogBuilder

                        .setCancelable(false)
                        .setPositiveButton("Cancel",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {



                            }
                          })
                        .setNegativeButton("Add",new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,int id) {

                                itemId=productname.getText().toString();
                                Height= height.getText().toString();
                                Width= width.getText().toString();
                                Image=imageForCart;
                                Log.e("item id",itemId+"");
                                Log.e("Height",Height+"");
                                Log.e("Width",Width+"");
                                Log.e("Image",Image+"");
                                AddToCart.add_to_class(itemId,Height,Width,Image);  
                                Toast.makeText(FullImageActivity2.this,"add successfully" , Toast.LENGTH_SHORT).show();
                                Intent opencart=new Intent(FullImageActivity2.this,CartList.class);
                                startActivity(opencart);


                            }


                        });

                        AlertDialog alertDialog = alertDialogBuilder.create();          
                        alertDialog.show();

任何帮助?

3 个答案:

答案 0 :(得分:0)

将layout参数设置为heightTxt和widthTxt

答案 1 :(得分:0)

尝试计算屏幕尺寸并根据屏幕尺寸选择值,而不是对lparams的值进行硬编码。

答案 2 :(得分:0)

尝试为您的Edittext提供LayoutWeight为1.它将覆盖所有剩余空间。 如果您将EditWeight提供给两个Edittexts,那么他们将平均分配剩余空间。

现在您的代码正在发生的是它为TextView of Height提供LayoutWeight 1。所以该行中的所有其他组件都占用了最小的空间,其余部分被高度TextView占用。

如果你想让它们的大小相等,那么将LayoutWeight全部提供为1。