这里是我的布局xml中定义RelativeLayout的代码片段。
这里是我保存对它的引用的位置。
<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);
后来当我试图将高度从0dp
更改为wrap_content
时,我这样获取布局参数:
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参数对象?