RelativeLayout孩子不会对齐吗?

时间:2015-08-13 11:27:36

标签: android relativelayout

这是我用来尝试将子元素与父元素右侧对齐的代码,为什么这段代码不起作用?

Context context = getApplicationContext();

RelativeLayout headingEnd = new RelativeLayout(this.context);
RelativeLayout.LayoutParams headingEndParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams headingEndTextParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
headingEnd.setBackgroundColor(Color.WHITE);
headingEnd.setId(1);
headingEnd.setGravity(Gravity.RIGHT);
headingEndParams.width = 200;
headingEndParams.height = 50;
headingEndParams.addRule(RelativeLayout.ALIGN_PARENT_END,-1);

headingEnd.addView(headingEnd, headingEndParams);

1 个答案:

答案 0 :(得分:0)

我的猜测是缺少了锚ID。而不是

headingEndParams.addRule(RelativeLayout.ALIGN_PARENT_END,-1);

我会尝试

headingEndParams.addRule(RelativeLayout.ALIGN_PARENT_END,R.id.idOfViewWhichShouldBeAlignedAtParent);

这只是一个想法。