android布局 边距,如何设置编辑文本框的布局边距?

在表格布局中我有一个tablerow,在该tablerow中我有6个编辑文本框,我想为这6个编辑文本框设置布局边距

TableLayout t1=(TableLayout)findViewById(R.id.table_layout01); TableRow tr1=new TableRow(inventory.this); tr1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); tr1.setBackgroundColor(Color.BLACK); EditText ed6=new EditText(inventory.this); //ed6.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); /*ViewGroup.MarginLayoutParams editmargin=new ViewGroup.MarginLayoutParams(ViewGroup.MarginLayoutParams.FILL_PARENT,ViewGroup.MarginLayoutParams.WRAP_CONTENT); editmargin.setMargins(leftMargin, rightMargin, topMargin, bottomMargin);*/ ed6.setTextColor(Color.BLACK); ed6.setBackgroundColor(Color.WHITE); ed6.setText("1"); tr1.addView(ed6); EditText ed7=new EditText(inventory.this); //ed7.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ed7.setTextColor(Color.BLACK); ed7.setBackgroundColor(Color.WHITE); ed7.setText("2"); tr1.addView(ed7); EditText ed8=new EditText(inventory.this); //ed8.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ed8.setTextColor(Color.BLACK); ed8.setBackgroundColor(Color.WHITE); ed8.setText("3"); tr1.addView(ed8); EditText ed9=new EditText(inventory.this); //ed9.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ed9.setTextColor(Color.BLACK); ed9.setBackgroundColor(Color.WHITE); ed9.setText("4"); tr1.addView(ed9); EditText ed10=new EditText(inventory.this); //ed10.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ed10.setTextColor(Color.BLACK); ed10.setText("5"); ed10.setBackgroundColor(Color.WHITE); tr1.addView(ed10); EditText ed11=new EditText(inventory.this); //ed11.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT)); ed11.setTextColor(Color.BLACK); ed11.setText("6"); ed11.setBackgroundColor(Color.WHITE); tr1.addView(ed11); t1.addView(tr1);

首先你应该知道的事情:根据官方的Android开发页面 ,视图(和TextView派生自View)不支持Margin的设置,但ViewGroups(如LinearLayout , RelativeLayout等…)可以。

那你可以做的是以下几点:

TableLayout.LayoutParams params = new TableLayout.LayoutParams(); params.setMargins(5, 5, 5, 5); TextView view = new TextView(this); view.setLayoutParams(params);

这会将所有孩子的边距设置为5像素 – 我尝试了它并且它对我有效(尽管有一个垂直对齐的LinearLayout )。 试一试,让我知道我是否可以进一步帮助:)。

干杯,

Ready4Fajir

编辑:

我会尝试下面的XML(你当然会更新id等)。 xml中的“魔力”是它在TextView (和第二行的EditText之间均匀分配所有可用宽度。

在Java代码中,您可以访问EditText视图,如:

EditText editText01 = (EditText) findViewById(R.id.editText01); editText01.setText("1");

我现在忽略了你需要以编程方式创建EditText的事实。 你真的 真的需要用Java创建它们吗? (为什么?)

老答案:

如果您只想将布局边距设置为EditText视图,我可以在LayoutParamsvariables上使用setMargins(left, top, right, bottom)函数调用。

int left = 6; int top = 12; int right = 6; int bottom = 6; TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); params.setMargins(left, top, right, bottom); EditText edXY = new EditText(inventory.this); edXY.setLayoutParams(params);

如果您最终希望在表格行的六个EditText视图中均匀分配所有可用空间,我建议您查看以下post: 2列TableLayout,每列精确50%

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值