我正在使用for循环动态添加TextView
LinearLayout
哪个方向是水平的。如果它到达屏幕宽度的末尾,是否可以将TextView
与第二行对齐?
答案 0 :(得分:3)
您可以通过以下方式完成此操作:
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
LinearLayout layout = new LinearLayout(this);
LinearLayout layout1 = new LinearLayout(this);
LinearLayout layout2 = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout1.setOrientation(LinearLayout.HORIZONTAL);
layout2.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 3; i++) {
TextView lblView = new TextView(this);
TextView lblView2 = new TextView(this);
lblView.setLayoutParams(params);
lblView.setText("hello");
lblView.setEms(5);
lblView.setGravity(Gravity.CENTER);
lblView2.setLayoutParams(params);
lblView2.setText("hello");
lblView2.setEms(5);
lblView2.setGravity(Gravity.CENTER);
layout1.addView(lblView);
layout2.addView(lblView2);
}
layout.addView(layout1);
layout.addView(layout2);
setContentView(layout);
答案 1 :(得分:0)
以下将解决这个问题,
textview.setLayoutParams(new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));