2013-01-13 17 views
1

我正在面對將動態創建的relativeLayout項目添加到其他項下的問題。我無法看到添加到它的第一個元素。每次我只能看到添加到它的最後一個元素。
我在這裏提供這個郵件的Java源代碼& xml文件。請幫我整理出這個問題:
無法動態地將項目添加到下方的RelativeLayout之一

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

嘗試通過一個視圖的id,假設上面是這樣的:x.addRule(RelativeLayout.BELOW,R.id.some_view); – EvZ

+0

我試圖在兩個地方添加下面的語句,但沒有找到任何改進。 lp_btn.addRule(RelativeLayout.BELOW,horizo​​ntalllayout.getId()); –

回答

2

//添加ID到您的horizo​​ntalllayout視圖

horizontalllayout.setId(100); 

//然後調用

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

//最後最後添加到您的滾動視圖

sview.addView(dynamiclayout); 
+0

我在兩處都添加了horizo​​ntalllayout.setId(100)&horizo​​ntalllayout.setId(200),但仍無法看到第一個視圖。 –

+0

需要爲第二個設置「lp_btn.addRule(RelativeLayout.BELOW,horizo​​ntalllayout.getId()); 」。不適用於第一個水平佈局。那麼問題就會被修復。 –

相關問題