Android 如何以编程方式在RelativeLayout中添加LinearLayout?
我有一个由ImageView和TextView组成的RelativeLayout。现在,我想在这个相对视图中添加一个LinearLayout,它应该在TextView下面对齐。现在,LinearLayout已添加到RelativeLayout,但它未在TextView下方对齐。这是我的密码:Android 如何以编程方式在RelativeLayout中添加LinearLayout?,android,android-layout,Android,Android Layout,我有一个由ImageView和TextView组成的RelativeLayout。现在,我想在这个相对视图中添加一个LinearLayout,它应该在TextView下面对齐。现在,LinearLayout已添加到RelativeLayout,但它未在TextView下方对齐。这是我的密码: void addRatingToImage(RelativeLayout relativelLayout, Movies movieObject) { ImageView ratingImageVi
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);
}
void添加到图像(RelativeLayout RelativeLayout,Movies-movieObject){
图像视图比率图像视图;
LinearLayout ratingLayout=新的LinearLayout(mContext);
额定布局。设置方向(线性布局。水平);
double-roundedRating=roundUpRating(double.parseDouble(movieObject.mRating));
对于(inti=0;i<5;i++){//TODO:检查
评级图像视图=新图像视图(mContext);
如果(i
我有一个疑问。当RelativeLayout.BELOW应用于嵌套在RelativeLayout中的不同类型的布局时,它是否起作用?谢谢。是的,
下面的相对位置将起作用。无论子视图类是什么,它都由父视图识别
根据您的问题,我假设RelativeLayout
是这样的,因为您已经为TextView movieNameTextView
的布局宽度设置了fill\u parent
。是的,RelativeLayout.BELOW
将起作用。无论子视图类是什么,它都由父视图识别
根据您的问题,我认为RelativeLayout
的行为是这样的,因为您已经为TextView movieNameTextView
的布局宽度设置了fill\u parent
。谢谢您通知RelativeLayout.Below将起作用。我通过使用RelativeLayout.ALIGN_PARENT_BOTTOM将LinearLayout与RelativeLayout对齐,然后使用边距解决了这个问题。但是,TextView movieNameTextView设置为包装内容。我会弄明白的。再次感谢。感谢您通知RelativeLayout。以下将起作用。我通过使用RelativeLayout.ALIGN_PARENT_BOTTOM将LinearLayout与RelativeLayout对齐,然后使用边距解决了这个问题。但是,TextView movieNameTextView设置为包装内容。我会弄明白的。再次感谢。