忽略Android RelativeLayout参数

忽略Android RelativeLayout参数,android,android-relativelayout,android-custom-view,Android,Android Relativelayout,Android Custom View,我遇到了一个奇怪的问题: 我有一个从RelativeLayout扩展而来的类 然后我想这样添加两个视图: mButtonTip = new ButtonTip(context, attrs); mToolTip = new ToolTip(context, attrs); RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WR

我遇到了一个奇怪的问题: 我有一个从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视图放置正确,但mbutontip不会从布局的左下角移动


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

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

我需要明确设置不同视图的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);

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

我需要明确设置不同视图的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);