1

我遇到了一个奇怪的问题:我有一个从 RelativeLayout 扩展的类。

然后我想像这样添加两个视图:

mButtonTip = new ButtonTip(context, attrs);
mToolTip = new ToolTip(context, attrs);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_BOTTOM, mToolTip.getId());

addView(mToolTip, params1);
addView(mButtonTip, params2);

不幸的是,mToolTip 视图已正确放置,但 mButtonTip 不会从布局的左下角移动。

您知道为什么 mButtonTip 视图忽略了我刚刚设置的布局参数吗?

4

1 回答 1

1

好的,所以对于那些可能遇到同样问题的人,我找到了一个解决方案:

我需要像这样明确设置不同视图的 id:

mButtonTip = new ButtonTip(context, attrs);
mButtonTip.setId(1);

mToolTip = new ToolTip(context, attrs);
mToolTip.setId(2);

RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params1.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_BOTTOM, mToolTip.getId());

addView(mToolTip, params1);
addView(mButtonTip, params2);
于 2013-04-02T10:25:08.073 回答