2013-01-13 4 views
1

동적으로 생성 된 relativeLayout에 대해 항목을 하나씩 추가 할 때 문제가 있습니다. 나는 그것에 추가 된 첫 번째 요소를 볼 수 없습니다. 매번 마지막 요소 만 추가 할 수 있습니다.
나는이 메일 자바 소스 코드 & xml 파일을 여기 제공하고있다. // 마지막으로동적으로 항목을 RelativeLayout에 추가 할 수 없습니다.

horizontalllayout.setId(100); 

// 다음

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

전화

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, horizontalalllayout.getId()); –

답변

2

이 horizontalllayout보기에 ID를 추가 //이 문제를 밖으로 정렬에 제발 도와주세요 끝에서 스크롤보기에 추가

sview.addView(dynamiclayout); 
+0

두 위치에서 horizontalalllayout.setId (100) 및 horizontalalllayout.setId (200)를 추가했지만 첫 번째보기를 여전히 볼 수 없었습니다. –

+0

두 번째로 "lp_btn.addRule (RelativeLayout.BELOW, horizontalalllayout.getId()); "을 설정해야합니다. 첫 수평 레이아웃이 아닙니다. 그런 다음 문제가 해결됩니다. –

관련 문제