Jay*_*amy 1 android dynamic android-layout
我的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>
Run Code Online (Sandbox Code Playgroud)
我使用以下代码在循环中创建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);
}
Run Code Online (Sandbox Code Playgroud)
我Button在TextView下面.谁能帮我更换这LinearLayout与RelativeLayout让我得到了TextView和Button并排?
修改您的布局,以便您拥有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>
Run Code Online (Sandbox Code Playgroud)
然后尝试这个使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);
        }
    }
Run Code Online (Sandbox Code Playgroud)
        |   归档时间:  |  
           
  |  
        
|   查看次数:  |  
           8466 次  |  
        
|   最近记录:  |