Android开发中的TableLayout
我在 TableLayout 中遇到问题,宽度问题。我尝试了很多搜索,但我得到的只是使用 xml 的解决方案。我正在以编程方式执行此操作。这就是我正在做的事情,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow tableTitle = new TableRow(this);
tableTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow name = new TableRow(this);
TableRow password = new TableRow(this);
TableRow button = new TableRow(this);
TextView empty = new TextView(this);
TextView title = new TextView(this);
title.setText("Hello table layout");
title.setGravity(Gravity.CENTER_HORIZONTAL);
TextView txtUname = new TextView(this);
txtUname.setText("Username");
TextView txtPass = new TextView(this);
txtPass.setText("Password");
EditText uname = new EditText(this);
EditText pass = new EditText(this);
Button login = new Button(this);
login.setText("Login");
name.addView(txtUname);
name.addView(uname);
password.addView(txtPass);
password.addView(pass);
button.addView(empty);
button.addView(login);
table.addView(name);
table.addView(password);
table.addView(button);
table.setColumnShrinkable(0, true);
setContentView(table);
}
我想缩小第一列。我应该在这段代码中添加什么?
有谁帮忙啊!
I am having issues in TableLayout, width issues. I tried searching a lot, but all I get is solution using xml. I am doing it programmatically. Here is what I am doing
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TableLayout table = new TableLayout(this);
table.setStretchAllColumns(true);
table.setShrinkAllColumns(true);
TableRow tableTitle = new TableRow(this);
tableTitle.setGravity(Gravity.CENTER_HORIZONTAL);
TableRow name = new TableRow(this);
TableRow password = new TableRow(this);
TableRow button = new TableRow(this);
TextView empty = new TextView(this);
TextView title = new TextView(this);
title.setText("Hello table layout");
title.setGravity(Gravity.CENTER_HORIZONTAL);
TextView txtUname = new TextView(this);
txtUname.setText("Username");
TextView txtPass = new TextView(this);
txtPass.setText("Password");
EditText uname = new EditText(this);
EditText pass = new EditText(this);
Button login = new Button(this);
login.setText("Login");
name.addView(txtUname);
name.addView(uname);
password.addView(txtPass);
password.addView(pass);
button.addView(empty);
button.addView(login);
table.addView(name);
table.addView(password);
table.addView(button);
table.setColumnShrinkable(0, true);
setContentView(table);
}
I want to shrink first column. What should I add in this code?
Anyone help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该考虑将您的布局移动到 xml 文件。这会让你的生活变得更轻松。
要仅使第一列可收缩,请替换
setShrinkAllColumns(true)
使用
setColumnShrinkable(0, true)
。You should consider moving your to layout xml file. This would make your life way easier.
To make only first column shrinkable replace
setShrinkAllColumns(true)
with
setColumnShrinkable(0, true)
.