2015-06-07 56 views
4

以下是我的布局xml中定义我的RelativeLayout的片段。RelativeLayout正在返回LinearLayout LayoutParams

<RelativeLayout 
    android:id="@+id/npvirrButtons" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    > 
    <Button 
     android:id="@+id/buttonNPVLabel" 
     android:layout_marginLeft="20dp" 
     android:layout_alignParentLeft="true" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     /> 

    <Button 
     android:id="@+id/buttonIRR_NFV" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_alignParentRight="true" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     /> 

</RelativeLayout> 

这里是我保存对它的引用。

this.npvirrButtons = (RelativeLayout)this.findViewById(R.id.npvirrButtons); 

再后来,当我尝试改变高度从0dpwrap_content,我抢布局PARAMS这样的:

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.npvirrButtons.getLayoutParams(); 

,但我得到抛出的异常:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams 
    at com.mydomain.myapp.MyActivity.calculateIRR(MyActivity.java:564) 

为什么我的RelativeLayout返回一个LinearLayout params对象?

+0

尝试在删除gen&bin文件夹后重新运行项目 –

回答

5

getLayoutParams()根据其父项返回视图的当前LayoutParameters。所以,buttonIRR_NFV将返回RelativeLayout.LayoutParameter

想想这样:getLayoutParams()是您调用的方法来改变当前视图在其容器上的位置。如果您想修改布局其中一个孩子的方式,则必须改为使用LayoutParameters。

相关问题