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

是的,RelativeLayout.BELOW会工作。RelativeLayout无论子视图类是什么,它都被父级识别。

根据你的问题,我想这RelativeLayout是因为你已经设置fill_parent了你TextView movieNameTextView的布局宽度。

于 2012-04-09T08:02:47.587 回答