1

我正在TextView使用 for 循环动态添加LinearLayout方向是水平的。TextView如果到达屏幕宽度的末端,是否可以将第二行对齐?

4

2 回答 2

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);
于 2013-08-30T06:37:38.707 回答
0

以下将做的伎俩,

textview.setLayoutParams(new LayoutParams(
                            LayoutParams.MATCH_PARENT,
                            LayoutParams.WRAP_CONTENT));
于 2013-08-30T05:59:59.230 回答