陷入addview

时间:2012-08-02 11:40:23

标签: android android-layout android-widget

LinearLayout.LayoutParams labellayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LinearLayout.LayoutParams textlayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
    for(int i=0 ;i < 2;i++){
        TextView label = new TextView(this);
        EditText text = new EditText(this);

        labellayoutParams.setMargins(40, 30, 30 , 0);
        textlayoutParams.setMargins(60, 30, 30 , 0);
        text.setHint("Type Here");
        label.setText("some text");

        ll.addView(label);
        ll.addView(text);
    }
    this.setContentView(ll);

上面的代码将输出显示为

注意:我们假设某个文本作为textview, _ __ _ 作为edittext

some text ____________ some text _____________

但我想要这样的输出

some text ____________

some text ____________

我尝试了setOrientation(LinearLayout.VERTICAL),但它给出了喜欢这个

some text 

_____________

some text

_____________

4 个答案:

答案 0 :(得分:1)

名为ll的LinearLayout应将其属性“android:orientation”设置为“vertical”

答案 1 :(得分:0)

您必须设置linearlayout方向ll.setorientation(VERTICAL)才能使文本视图垂直排列

答案 2 :(得分:0)

如果您调整垂直布局的方向,它将正常工作....

答案 3 :(得分:0)

尝试使用不同的布局...我认为它不能在水平和垂直方向上添加到单个布局中(在LinearLayout中)。你试图在这里做..