0

ImageView と TextView で構成される RelativeLayout があります。さて、この RelativeLayout に LinearLayout を追加したいと思います。これは TextView の下に配置する必要があります。現在、LinearLayout は RelativeLayout に追加されていますが、TextView の下に配置されていません。これが私のコードです:

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 にネストされた別の種類のレイアウトに適用された場合に機能しますか? ありがとう。

4

1 に答える 1

3

Yes, RelativeLayout.BELOW will work. It is recognized by parent RelativeLayout no matter what the child view class is.

According to your problem, I suppose that RelativeLayout is behaving that way because you've set fill_parent for your TextView movieNameTextView's layout width.

于 2012-04-09T08:02:47.587 に答える