无法动态地将项目添加到RelativeLayout以下的其他项目

时间:2013-01-13 15:26:42

标签: android android-layout

我在为动态创建的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>

1 个答案:

答案 0 :(得分:2)

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

horizontalllayout.setId(100);

//然后调用

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

//最后在最后添加到滚动视图

sview.addView(dynamiclayout);