2015-01-05 3 views
2

내 애플리케이션의 상대 레이아웃을 동적으로 생성하는 방법을 알고 싶습니다. 나는 서버에서 얻은 각 항목에 대해 새로운 상대 레이아웃을 생성하려고합니다.안드로이드에서 상대 레이아웃을 동적으로 생성하기

다음과 같이 내가 생성 할

내 XML 뷰는 다음과 같습니다

<RelativeLayout 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:background="#FFFFFF" 
tools:context=".MainActivity" 
android:id="@+id/Main_Layout"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:id="@+id/question_content" 
    > 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/questions"> 
     <!-- Month and Year on the top --> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="#22C778" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="December 2014" 
      android:gravity="center" 
      android:id="@+id/month_name" 
      android:padding="5dp" 
      android:textColor="#FFFFFF" 
      android:layout_alignParentTop="true" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" /> 

     <!-- Date and Month On The Left --> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:text="30 Jan" 
      android:gravity="center_vertical" 
      android:background="#F1F1F1" 
      android:padding="10dp" 
      android:layout_marginTop="5dp" 
      android:id="@+id/date_text" 
      android:layout_below="@+id/month_name" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentStart="true" 
      /> 

     <!-- Question Title --> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="#FFFFFF" 
      android:padding="5dp" 
      android:text="This Is The Title Of Question Of January" 
      android:id="@+id/question_title" 
      android:layout_alignTop="@+id/date_text" 
      android:layout_toRightOf="@+id/date_text" /> 

     <!-- Question Description --> 
     <TextView 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:padding="10dp" 
      android:text="This Is The Description Of The Question You See Above And Below Is The Rating Bar" 
      android:id="@+id/question_desc" 
      android:layout_below="@+id/question_title" 
      android:layout_toRightOf="@+id/date_text" 
      android:layout_toEndOf="@+id/date_text" /> 

     <RatingBar 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/question_rating" 
      android:numStars="1" 
      android:stepSize="1" 
      android:layout_marginLeft="10dp" 
      android:layout_below="@+id/question_desc" 
      android:layout_alignLeft="@+id/question_desc" 
      android:layout_alignStart="@+id/question_desc" 

      /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textAppearance="?android:attr/textAppearanceSmall" 
      android:text="0 people rated this question" 
      android:id="@+id/people_rated" 
      android:padding="15dp" 
      android:layout_alignBottom="@id/question_rating" 
      android:layout_below="@+id/question_desc" 
      android:layout_toRightOf="@id/question_rating" /> 
    </RelativeLayout> 

내가 자바 코드에서이 일을 시도 : 나는 안드로이드 내부의 레이아웃을 만들려면 : ID를 = "@ + ID/question_content "동적으로

RelativeLayout Questions_Layout=new RelativeLayout(this); 
    RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
      RelativeLayout.LayoutParams.WRAP_CONTENT); 

    for(int i=0;i<4;i++) 
    { 
     //Setting Parameters For TextViews 
     //Month and Year TextView " DEC 2014 

     TextView month_name = new TextView(this); 
     month_name.setText("Jan 2014"); 
     month_name.setId(R.id.ques_title); 
     month_name.setPadding(5, 5, 5, 5); 
     month_name.setGravity(Gravity.CENTER); 
     month_name.setBackgroundColor(Color.parseColor("#22C778")); 
     month_name.setTextColor(Color.parseColor("#FFFFFF")); 
     month_name.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
     params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
     params.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE); 
     month_name.setLayoutParams(params); 


     //Adding Text View To Relative Layout 
     Questions_Layout.addView(month_name); 


     //Setting Parameters For TextViews 
     //Date and Month TextView " 12 DEC 
     TextView Day_Month= new TextView(this); 

     Day_Month.setText("12 Jan"); 
     Day_Month.setId(R.id.day_month); 
     Day_Month.setPadding(10, 10, 10, 10); 
     params.setMargins(0, 5, 0, 0); 
     Day_Month.setGravity(Gravity.CENTER_VERTICAL); 
     Day_Month.setBackgroundColor(Color.parseColor("#F1F1F1")); 
     Day_Month.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
     params.removeRule(RelativeLayout.ALIGN_PARENT_TOP); 
     params.addRule(RelativeLayout.BELOW,R.id.month_year); 
     Day_Month.setLayoutParams(params); 
     Questions_Layout.addView(Day_Month); 

     //Setting Parameters For TextViews 
     //Question Title TextView "This Is Title Of The Question 

     TextView Ques_Title=new TextView(this); 
     Ques_Title.setId(R.id.ques_title); 
     Ques_Title.setText("This Is The Title"); 
     Ques_Title.setPadding(5, 5, 5, 5); 
     Ques_Title.setBackgroundColor(Color.parseColor("#FFFFFF")); 
     params.removeRule(RelativeLayout.BELOW); 
     params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     params.removeRule(RelativeLayout.ALIGN_PARENT_START); 
     params.addRule(RelativeLayout.ALIGN_TOP,R.id.day_month); 
     params.addRule(RelativeLayout.RIGHT_OF,R.id.day_month); 
     Ques_Title.setLayoutParams(params); 
     Questions_Layout.addView(Ques_Title); 
     //Setting Parameters For TextViews 
     //Question Description TextView "This Is Description Of The Question 

     TextView Ques_Desc=new TextView(this); 
     Ques_Desc.setId(R.id.ques_desc); 
     Ques_Desc.setText("This Is The Description Of Question"); 
     Ques_Desc.setPadding(10, 10, 10, 10); 
     Ques_Desc.setBackgroundColor(Color.parseColor("#FFFFFF")); 
     params.removeRule(RelativeLayout.RIGHT_OF); 
     params.removeRule(RelativeLayout.ALIGN_TOP); 
     params.addRule(RelativeLayout.BELOW,R.id.ques_title); 
     params.addRule(RelativeLayout.RIGHT_OF,R.id.day_month); 
     params.addRule(RelativeLayout.END_OF,R.id.day_month); 
     Ques_Desc.setLayoutParams(params); 
     Questions_Layout.addView(Ques_Desc); 


     RatingBar ratingBar=new RatingBar(this); 
     ratingBar.setId(R.id.rating_bar); 
     ratingBar.setNumStars(1); 
     ratingBar.setStepSize(1); 
     params.setMargins(10,0,0,0); 
     params.removeRule(RelativeLayout.ALIGN_LEFT); 
     params.removeRule(RelativeLayout.BELOW); 
     params.removeRule(RelativeLayout.START_OF); 
     params.addRule(RelativeLayout.ALIGN_LEFT,R.id.ques_desc); 
     params.addRule(RelativeLayout.BELOW,R.id.ques_desc); 
     params.addRule(RelativeLayout.START_OF,R.id.ques_desc); 
     ratingBar.setLayoutParams(params); 
     Questions_Layout.addView(ratingBar); 


     //Setting Parameters For TextViews 
     //People Rated TextView "3 people Rated This" 

     TextView People_Rated=new TextView(this); 
     People_Rated.setId(R.id.people_rated); 
     People_Rated.setText("9 People Rated This"); 
     People_Rated.setPadding(15, 15, 15, 15); 
     People_Rated.setBackgroundColor(Color.parseColor("#FFFFFF")); 
     params.removeRule(RelativeLayout.RIGHT_OF); 
     params.removeRule(RelativeLayout.BELOW); 
     params.addRule(RelativeLayout.BELOW,R.id.ques_desc); 
     params.addRule(RelativeLayout.ALIGN_TOP,R.id.rating_bar); 
     params.addRule(RelativeLayout.RIGHT_OF,R.id.rating_bar); 
     People_Rated.setLayoutParams(params); 
     Questions_Layout.addView(People_Rated); 




    } 
    LinearLayout base=(LinearLayout) findViewById(R.id.question_content); 
    base.addView(Questions_Layout); 

    // LinearLayout main=(LinearLayout) findViewById(R.id.question_content); 
    // main.addView(sv); 
    setContentView(R.layout.activity_main); 
    ViewGroup container = (ViewGroup)findViewById(R.id.question_content); 
    container.addView(Questions_Layout); 

하지만 작동하지 않습니다. 내가 뭔가 잘못하고있는거야. 제발 뭔가를 제안

+0

코드에서 작동하지 않는 것은 무엇입니까? 현재 무슨 일이 일어나고 있습니까? [재사용 가능한 레이아웃 살펴보기] (http://developer.android.com/training/improving-layouts/reusing-layouts.html)를 권장하고 필요에 따라 추가하십시오. 당신이 보여준 방향은 관리가 어려울 수있는 것처럼 보입니다. – CodeMonkey

+0

내가 Question_Layout의 각 하위 요소로 전달하는 매개 변수를 추측합니다. 즉, textViews가 오류를 표시하고 있습니다. 또한 내가 try catch 블록을 사용할 때 ArrayIndexOutOfBound 오류라고 말합니다. –

+0

'ArrayIndexOutOfBound'는 매우 구체적입니다. LogCat을 게시하고 오류의 원인이되는 코드 줄을 지적 할 수 있습니까? – CodeMonkey

답변

0

setContentView (R.layout.activity_main); 는 LinearLayout before이어야합니다. base = (LinearLayout) findViewById (R.id.question_content); 그렇지 않으면 base는 value를 null로 반환합니다.

더 많은 문제가 있습니다. 추가 할 때마다 params 객체를 만들고, for 루프 외부에서 Questions_Layout보기를 만듭니다. inside for 루프 및 base.addView (Questions_Layout);이어야합니다. for 루프 안에 있어야합니다. 정답을 확인하십시오.

setContentView(R.layout.activity_main); 

LinearLayout base=(LinearLayout) findViewById(R.id.question_content); 

for(int i=0;i<4;i++) 
{ 
    RelativeLayout Questions_Layout=new RelativeLayout(this); 

    //Setting Parameters For TextViews 
    //Month and Year TextView " DEC 2014 

    TextView month_name = new TextView(this); 
    month_name.setText("Jan 2014"); 
    month_name.setId(R.id.ques_title); 
    month_name.setPadding(5, 5, 5, 5); 
    month_name.setGravity(Gravity.CENTER); 
    month_name.setBackgroundColor(Color.parseColor("#22C778")); 
    month_name.setTextColor(Color.parseColor("#FFFFFF")); 
    RelativeLayout.LayoutParams params=new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    month_name.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
    params.addRule(RelativeLayout.ALIGN_PARENT_START, RelativeLayout.TRUE); 
    //Adding Text View To Relative Layout 
    Questions_Layout.addView(month_name); 
    month_name.setLayoutParams(params); 




    //Setting Parameters For TextViews 
    //Date and Month TextView " 12 DEC 
    TextView Day_Month= new TextView(this); 

    Day_Month.setText("12 Jan"); 
    Day_Month.setId(R.id.day_month); 
    Day_Month.setPadding(10, 10, 10, 10); 
    params.setMargins(0, 5, 0, 0); 
    Day_Month.setGravity(Gravity.CENTER_VERTICAL); 
    Day_Month.setBackgroundColor(Color.parseColor("#F1F1F1")); 
    Day_Month.setTextAppearance(this, android.R.style.TextAppearance_Medium); 
    params=new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.removeRule(RelativeLayout.ALIGN_PARENT_TOP); 
    params.addRule(RelativeLayout.BELOW,R.id.month_year); 
    Questions_Layout.addView(Day_Month); 
    Day_Month.setLayoutParams(params); 

    //Setting Parameters For TextViews 
    //Question Title TextView "This Is Title Of The Question 

    TextView Ques_Title=new TextView(this); 
    Ques_Title.setId(R.id.ques_title); 
    Ques_Title.setText("This Is The Title"); 
    Ques_Title.setPadding(5, 5, 5, 5); 
    Ques_Title.setBackgroundColor(Color.parseColor("#FFFFFF")); 
    params=new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.removeRule(RelativeLayout.BELOW); 
    params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    params.removeRule(RelativeLayout.ALIGN_PARENT_START); 
    params.addRule(RelativeLayout.ALIGN_TOP,R.id.day_month); 
    params.addRule(RelativeLayout.RIGHT_OF,R.id.day_month); 
    Questions_Layout.addView(Ques_Title); 
    Ques_Title.setLayoutParams(params); 
    //Setting Parameters For TextViews 
    //Question Description TextView "This Is Description Of The Question 

    TextView Ques_Desc=new TextView(this); 
    Ques_Desc.setId(R.id.ques_desc); 
    Ques_Desc.setText("This Is The Description Of Question"); 
    Ques_Desc.setPadding(10, 10, 10, 10); 
    Ques_Desc.setBackgroundColor(Color.parseColor("#FFFFFF")); 
    params=new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.removeRule(RelativeLayout.RIGHT_OF); 
    params.removeRule(RelativeLayout.ALIGN_TOP); 
    params.addRule(RelativeLayout.BELOW,R.id.ques_title); 
    params.addRule(RelativeLayout.RIGHT_OF,R.id.day_month); 
    params.addRule(RelativeLayout.END_OF,R.id.day_month); 
    Questions_Layout.addView(Ques_Desc); 
    Ques_Desc.setLayoutParams(params); 


    RatingBar ratingBar=new RatingBar(this); 
    ratingBar.setId(R.id.rating_bar); 
    ratingBar.setNumStars(1); 
    ratingBar.setStepSize(1); 
    params=new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.setMargins(10,0,0,0); 
    params.removeRule(RelativeLayout.ALIGN_LEFT); 
    params.removeRule(RelativeLayout.BELOW); 
    params.removeRule(RelativeLayout.START_OF); 
    params.addRule(RelativeLayout.ALIGN_LEFT,R.id.ques_desc); 
    params.addRule(RelativeLayout.BELOW,R.id.ques_desc); 
    params.addRule(RelativeLayout.START_OF,R.id.ques_desc); 
    Questions_Layout.addView(ratingBar); 
    ratingBar.setLayoutParams(params); 


    //Setting Parameters For TextViews 
    //People Rated TextView "3 people Rated This" 

    TextView People_Rated=new TextView(this); 
    People_Rated.setId(R.id.people_rated); 
    People_Rated.setText("9 People Rated This"); 
    People_Rated.setPadding(15, 15, 15, 15); 
    People_Rated.setBackgroundColor(Color.parseColor("#FFFFFF")); 
    params=new  RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
     RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.removeRule(RelativeLayout.RIGHT_OF); 
    params.removeRule(RelativeLayout.BELOW); 
    params.addRule(RelativeLayout.BELOW,R.id.ques_desc); 
    params.addRule(RelativeLayout.ALIGN_TOP,R.id.rating_bar); 
    params.addRule(RelativeLayout.RIGHT_OF,R.id.rating_bar); 
    Questions_Layout.addView(People_Rated); 
    People_Rated.setLayoutParams(params); 

    base.addView(Questions_Layout); 
} 

그러나 더 좋은 방법는 팽창기를 사용하는 것입니다. 모든 레이아웃을 동적으로 추가하는 대신 사용자 정의 행이있는 하나의 XML을 만들고이를 동적으로 확장 할 수 있습니다. 예를 들어, 레이아웃을 xml에 item_layout이라는 이름으로 저장했다고 가정 해 봅시다. 이제 레이아웃을 동적으로 추가 할 때 사용하십시오. 더 깨끗한 접근법입니다.

setContentView(R.layout.activity_main); 
ViewGroup container = (ViewGroup)findViewById(R.id.question_content); 

for(int i=0;i<4;i++){ 
    LayoutInflater inflate = LayoutInflater.from(mContext); 
    ViewGroup base = (ViewGroup)inflate.inflate(R.layout.item_layout, container, false); 

    TextView month_name = base.findViewById(R.id.month_name); 
    month_name.setText("Jan 2014"); 

    TextView Day_Month = base.findViewById(R.id.day_month); 
    Day_Month.setText("12 Jan"); 

    TextView Ques_Title=new TextView(R.id.ques_title); 
    Ques_Title.setText("This Is The Title"); 

    TextView Ques_Desc=new TextView(R.id.ques_desc); 
    Ques_Desc.setText("This Is The Description Of Question"); 

    RatingBar ratingBar=new RatingBar(R.id.rating_bar); 
    ratingBar.setNumStars(1); 

    TextView People_Rated=new TextView(R.id.day_month); 
    People_Rated.setText("9 People Rated This"); 

    container.addView(base); 
} 
+0

제안을 위해 thnks하지만 당신은 inflator에 대해 조금 더 생각할 수 있습니다 –

+0

Manush, 당신은 상대적 레이아웃과 4 개의 텍스트 뷰를 가질 수있는 하나의 XML을 생성 할 수 있습니다. xml 안에 모든 텍스트 색상, 중력을 추가하십시오. 위에 제시된대로 그것을 팽창 시키십시오. 내 편집 된 답장을 확인하십시오. – Manish

+0

안녕하세요 Manush,이 솔루션이 도움이 되었습니까, 아니면 여전히 문제가 발생 했습니까? 문제가 해결 된 경우이 솔루션을 수락하십시오. – Manish

관련 문제