1

我的layout.xml文件如下所示:

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="270dp"
        android:layout_weight="0.47" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>

    </ScrollView>

我使用以下代码在循环中创建TextViews 和Buttons :for

  View linearLayout =  findViewById(R.id.linearLayout2);


for (int i=0 ; i<10; i++){
     TextView textview = new TextView(this);
     textview.setText("Text view" + i);
     textview.setId(i);

     textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
     ((LinearLayout) linearLayout).addView(textview);


    Button button = new Button(this);
    button.setText("View");
    button.setId(i);
    int width=90;
    int height=60;
    button.setLayoutParams(new LayoutParams(width, height));
    ((LinearLayout) linearLayout).addView(button);

}

我得到Button下面的TextView。任何人都可以帮我替换它LinearLayoutRelativeLayout以便我并排得到TextView和吗?Button

4

4 回答 4

6

修改你的布局,让你有一个RelativeLayout而不是LinearLayout

<ScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="270dp"
        android:layout_weight="0.47" >

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
        </RelativeLayout>

</ScrollView>

然后试试这个让TextViews 和Buttons 坐在同一行:

    RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
    for (int i = 0; i < 10; i++) {
        Button button = new Button(this);
        button.setText("View" + i);
        button.setId(1000 + i);
        int width = 90;
        int height = 60;

        TextView textview = new TextView(this);
        textview.setText("Text view" + i);
        textview.setId(2000 + i);

        if (i == 0) {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            textview.setLayoutParams(rlp2);
            rl.addView(textview);
            RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                    width, height);
            rlp1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            button.setLayoutParams(rlp1);
            rl.addView(button);
        } else {
            RelativeLayout.LayoutParams rlp2 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            rlp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            rlp2.addRule(RelativeLayout.BELOW, button.getId() - 1);
            textview.setLayoutParams(rlp2);
            rl.addView(textview);
            RelativeLayout.LayoutParams rlp1 = new RelativeLayout.LayoutParams(
                    width, height);
            rlp1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
            rlp1.addRule(RelativeLayout.BELOW, button.getId() - 1);
            button.setLayoutParams(rlp1);
            rl.addView(button);
        }
    }
于 2012-04-11T18:58:44.723 回答
0

您正在使用 定义 LinearLayout android:orientation="vertical",因此它往往在另一个之下。

将您的文本视图和按钮添加到单个线性布局,方向为水平,并将此线性布局添加到原始垂直线性布局。

编辑:尝试以下操作:

View linearLayout =  findViewById(R.id.linearLayout2);

for (int i=0 ; i<10; i++){
 View indiLyt = new View(this);
 indiLyt.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
 indiLyt.setOrientation(LinearLayout.HORIZONTAL);

 TextView textview = new TextView(this);
 textview.setText("Text view" + i);
 textview.setId(i);

 textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
 ((LinearLayout) indiLyt).addView(textview);

 Button button = new Button(this);
 button.setText("View");
 button.setId(i);
 int width=90;
 int height=60;
 button.setLayoutParams(new LayoutParams(width, height));
 ((LinearLayout) indiLyt).addView(button);
 ((LinearLayout) linearLayout).addView(indiLyt);
}
于 2012-04-11T18:11:48.893 回答
0

试试这个解决方案::;

在项目的 res/layout 中有一个新的 xml 文件,如下所示。

将此另存为 linear.xml

< LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
            android:orientation="horizontal" >

        <TextView
            android:id="@+id/text"
            android:layout_width=" wrap_content "
            android:layout_height=" wrap_content " />
<Button            android:id="@+id/button"
            android:layout_width="90dp"
            android:layout_height="60dp" />
    </ LinearLayout >

和下面的代码:::

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.linearLayout2);
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);

for (int i=0 ; i<10; i++){
View view = inflater.inflate(R.layout.linear,null);
view.findViewById(R.id.text).setText("Text view" + i);
view.findViewById(R.id.text).setTextColor(Color.BLUE);
view.findViewById(R.id.button).setText(“View");
view.findViewById(R.id.button).setTextColor(Color.BLUE);
linearLayout.addView(view);
}
于 2012-04-11T18:20:02.170 回答
0
 View linearLayout =  findViewById(R.id.linearLayout2);


for (int i=0 ; i<10; i++){
     TextView textview = new TextView(this);
     textview.setText("Text view" + i);
     textview.setId(i);

     textview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
     ((LinearLayout) linearLayout).addView(textview);


    Button button = new Button(this);
    button.setText("View");
    button.setId(i);
    int width=90;
    int height=60;
    button.setLayoutParams(new LayoutParams(width, height));
    ((LinearLayout) linearLayout).addView(button);

}

用下面的代码替换上面的::

LinearLayout linearLayout =  (LinearLayout)findViewById(R.id.linearLayout2);
for (int i=0 ; i<10; i++){
LinearLayout l = new LinearLayout(this);
l.setOrientation(LinearLayout.HORIZONTAL);

TextView textview = new TextView(this);
textview.setText("Text view" + i);
textview.setId(i);

l.addView(textview);
Button button = new Button(this);
button.setText("View");
button.setId(i);
button.setWidth(90);
button.setHeight(60);
l.addView(button);
linearLayout.addView(l);//if you want you can layout params linearlayout
}
于 2012-04-11T17:42:12.563 回答