2012-04-09 4 views
0

ImageView와 TextView로 구성된 RelativeLayout을 사용했습니다. 이제이 RelativeLayout에 TextView 아래에 정렬되어야하는 LinearLayout을 추가하고 싶습니다. 지금은 LinearLayout이 RelativeLayout에 추가되었지만 TextView 아래에 정렬되어 있지 않습니다. 여기 내 코드는 다음과 같습니다.프로그래밍 방식으로 RelativeLayout 내부에 LinearLayout을 추가하려면 어떻게해야합니까?

void addRatingToImage(RelativeLayout relativelLayout, Movies movieObject) { 
    ImageView ratingImageView; 
    LinearLayout ratingLayout = new LinearLayout(mContext); 
    ratingLayout.setOrientation(LinearLayout.HORIZONTAL); 

    double roundedRating = roundUpRating(Double.parseDouble(movieObject.mRating)); 
    for(int i = 0; i < 5; i++) {      //TODO: Check 
     ratingImageView = new ImageView(mContext); 
     if(i < roundedRating) { 
      ratingImageView.setBackgroundResource(R.drawable.star_2); 

      ratingLayout.addView(ratingImageView); 
     } 
     else { 
      ratingImageView.setBackgroundResource(R.drawable.star_1); 
      ratingLayout.addView(ratingImageView); 
     } 

    } 

    RelativeLayout.LayoutParams ratingLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

    TextView movieNameTextView = (TextView)relativelLayout.getChildAt(2); 
    ratingLayoutParams.addRule(RelativeLayout.BELOW, movieNameTextView.getId()); 

    ratingLayout.setLayoutParams(ratingLayoutParams); 

    relativelLayout.addView(ratingLayout); 
} 

여기에 의심의 여지가 있습니다. RelativeLayout.BELOW가 다른 종류의 레이아웃에 적용될 때 RelativeLayout에 중첩됩니까? 감사.

답변

3

예, RelativeLayout.BELOW이 작동합니다. 자식보기 클래스가 무엇이든 관계없이 부모 RelativeLayout에 의해 인식됩니다.

귀하의 문제에 따르면 TextView movieNameTextView의 레이아웃 너비에 대해 fill_parent을 설정했기 때문에 RelativeLayout이 그런 식으로 행동한다고 ​​가정합니다.

+0

RelativeLayout.Below가 작동한다는 것을 알려 주셔서 감사합니다. RelativeLayout.ALIGN_PARENT_BOTTOM을 사용하여이를 해결하여 LinearLayout을 RelativeLayout에 정렬 한 다음 여백을 사용했습니다. 그러나 TextView movieNameTextView는 WRAP_CONTENT로 설정됩니다. 나는 그것을 알아낼 것이다. 다시 한번 감사합니다. – FireAndIce

관련 문제