2

我以编程方式使用相对布局。它有多个文本视图,但它显示水平而不是垂直。我如何以编程方式获取它们作为段落视图?

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


    int x=10;
    int y=30;
    for(int i=0;i<arrayList.size();i++)
    {
                   String str=arrayList.get(i);
        int strLength=arrayList.size();
        if(arrayList.get(i).equals("Name")){
            y=y+10;
            x=65;
        }
        else
        {
            x=x+strLength+60;
        }

        //tv=(TextView) findViewById(R.id.tv1);
        tv=new TextView(this);
        tv.setText(arrayList.get(i).toString());
        tv.setPadding(x, y, 0, 0);
        layout.addView(tv, params);

    }
     scroll.addView(layout);
    this.setContentView(scroll);
4

2 回答 2

2

RelativeLayout没有名为 的属性Orientation。您必须为每个人提供 idtextviews并添加这样的规则。

tv.addRule(RelativeLayout.BELOW, someOtherView.getId());

所以在你的情况下,我认为它会是这样的:

for(int i=0;i<arrayList.size();i++)
    {
        String str=arrayList.get(i);
        int strLength=arrayList.size();
        if(arrayList.get(i).equals("Name")){
            y=y+10;
            x=65;
        }
        else
        {
            x=x+strLength+60;
        }

        //tv=(TextView) findViewById(R.id.tv1);
                    tv=new TextView(this);
                    tv.setId(i);
                    tv.setText(arrayList.get(i).toString());
                         tv.setPadding(x, y, 0, 0);
                    layout.addView(tv, params);
                    if (i>0)
                        tv.addRule(RelativeLayout.BELOW, i-1);
    }

希望这可以帮助。

于 2013-06-27T06:32:17.537 回答
0

您只需将规则添加到参数RelativeLayout.BELOW

试试这样...

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
                    params.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);


        int x=10;
        int y=30;
        for(int i=0;i<arrayList.size();i++)
        {
                       String str=arrayList.get(i);
            int strLength=arrayList.size();
            if(arrayList.get(i).equals("Name")){
                y=y+10;
                x=65;
            }
            else
            {
                x=x+strLength+60;
            }

            //tv=(TextView) findViewById(R.id.tv1);
            tv=new TextView(this);
            tv.setText(arrayList.get(i).toString());
            tv.setPadding(x, y, 0, 0);
           if(layout.getChildCount() != 0)
                params.addRule(RelativeLayout.BELOW, (layout.getChildAt(layout.getChildCount() - 1)).getId());
            layout.addView(tv, params);

        }
         scroll.addView(layout);
        this.setContentView(scroll);

希望这可以帮助...

于 2013-06-27T06:42:19.613 回答