Android 如何在自定义对话框中修复对齐?
我尝试在android中创建一个自定义对话框,如图所示: 正如您所看到的,高度和编辑文本之间有一个很大的空间,如何使第二行中的所有组件向左或水平居中,以下是我的代码:Android 如何在自定义对话框中修复对齐?,android,dialog,customdialog,Android,Dialog,Customdialog,我尝试在android中创建一个自定义对话框,如图所示: 正如您所看到的,高度和编辑文本之间有一个很大的空间,如何使第二行中的所有组件向左或水平居中,以下是我的代码: AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(FullImageActivity2.this); final TextView title = new TextView(FullImageActivity2.
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();
有什么帮助吗 将布局参数设置为heightTxt和widthTxt而不是硬编码LPARAM的值,尝试计算屏幕大小并根据屏幕大小选择值。尝试将编辑文本的布局权重设置为1。它将覆盖所有剩余的空间。 如果将为两个编辑文本提供LayoutWeight,则它们将平均分配剩余空间 现在代码中发生的事情是,它为高度的文本视图提供了LayoutWeight 1。因此,该行中的所有其他组件占用了它们的最小空间,其余部分由Height TextView占用 如果要使它们的大小相等,请将LayoutWeight设置为1