Android Studio无法解析方法addRule()

时间:2014-03-02 15:18:22

标签: android layoutparams

ViewGroup.LayoutParams layoutParams = new RelativeLayout.LayoutParams(containerWidth, fragmentHeight);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

我得到:错误:找不到符号方法addRule(int)

Android Studio v 0.4.5

2 个答案:

答案 0 :(得分:3)

因为您使用了ViewGroup.LayoutParams

ViewGroup.LayoutParams layoutParams = new RelativeLayout.LayoutParams(containerWidth, fragmentHeight);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

addRule 方法在实现的子类RelativeLayout.LayoutParams中,所以您可能想要使用:

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(containerWidth, fragmentHeight);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

请参阅documentation of RelativeLayout.LayoutParams here

答案 1 :(得分:2)

应该是

RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(containerWidth, fragmentHeight);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);