1

我在为动态创建的 relativeLayout 添加项目时遇到了问题。我看不到添加到其中的第一个元素。每次我只能看到添加到其中的最后一个元素。
我在这里提供此邮件 java 源代码和 xml 文件。请帮我解决这个问题:

 DynamicRelativeLayoutActivity.java
 ==================================

   package com.andr.rlayout;



   import android.app.Activity;
   import android.graphics.Color;
   import android.os.Bundle;
     import android.view.ViewGroup.LayoutParams;
  import android.widget.RelativeLayout;
  import android.widget.ScrollView;
  import android.widget.TextView;

 public class DynamicRelativeLayoutActivity extends Activity {
/** Called when the activity is first created. */
RelativeLayout rLayout;
ScrollView sview;
RelativeLayout dynamiclayout;
LinearLayout horizontalllayout;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rlayout);
    sview = (ScrollView)findViewById(R.id.slayout);
    dynamiclayout = new RelativeLayout(DynamicRelativeLayoutActivity.this);
    dynamiclayout.setBackgroundColor(Color.WHITE);
    sview.addView(dynamiclayout);
    RelativeLayout.LayoutParams lp_btn;
    horizontalllayout = new LinearLayout(DynamicRelativeLayoutActivity.this);
    horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);

    TextView tv = new TextView(DynamicRelativeLayoutActivity.this);
    tv.setText("Hi");
    horizontalllayout.addView(tv);
    lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
    lp_btn.addRule(RelativeLayout.BELOW);
    dynamiclayout.addView(horizontalllayout, lp_btn);  


    horizontalllayout = new LinearLayout(DynamicRelativeLayoutActivity.this);
    horizontalllayout.setOrientation(android.widget.LinearLayout.HORIZONTAL);
    TextView tv1 = new TextView(DynamicRelativeLayoutActivity.this);
    tv1.setText("Hello");
    horizontalllayout.addView(tv1);
   lp_btn = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
            LayoutParams.WRAP_CONTENT);
  lp_btn.addRule(RelativeLayout.BELOW);
    dynamiclayout.addView(horizontalllayout, lp_btn);


   }
    }

  rlayout.xml
  ===========
  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
  >
<ScrollView 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/slayout"
>
 </ScrollView>
</RelativeLayout> 



  AndroidManifest.xml
   -==================
     <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.andr.rlayout"
       android:versionCode="1"
       android:versionName="1.0" >

   <uses-sdk android:minSdkVersion="10" />

    <application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:name=".DynamicRelativeLayoutActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

4

1 回答 1

2

//将id添加到你的horizo​​ntalllayout视图

horizontalllayout.setId(100);

//然后调用

lp_btn.addRule(RelativeLayout.BELOW,horizontalllayout.getId());

//最后添加到你的滚动视图

sview.addView(dynamiclayout);
于 2013-01-13T15:49:43.730 回答