设置RelativeLayout的布局PARAMS编程 - 抛出ClassCastException异常抛出、布局、异常、RelativeLayout

2023-09-03 21:28:04 作者:失败乃成功之母

我夸大从XML RelativeLayout的,然后试图以编程方式设置其布局PARAMS和我收到一个ClassCastException。

I am inflating a RelativeLayout from XML and then attempting to programmatically sets its layout params and am receiving a ClassCastException.

code:

profileHeader = (RelativeLayout)mInflater.inflate(R.layout.user_profile_header, null);
profileHeader.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));

抛出

E/AndroidRuntime( 2963): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
E/AndroidRuntime( 2963):  at android.widget.ListView.measureScrapChild(ListView.java:1119)
E/AndroidRuntime( 2963):  at android.widget.ListView.measureHeightOfChildren(ListView.java:1202)
E/AndroidRuntime( 2963):  at android.widget.ListView.onMeasure(ListView.java:1111)
E/AndroidRuntime( 2963):  at android.view.View.measure(View.java:8222)
E/AndroidRuntime( 2963):  at android.view.ViewGroup.measureChildWithMargins(V

我看到网上其他注意事项尝试使用LinearLayout.LayoutParams但似乎没有帮助。

I've seen other notes on the net to try using LinearLayout.LayoutParams but that doesnt seem to help.

如果你想知道为什么我做这一切时,不只是设置它在XML的,因为我看到一些奇怪在布局跳跃到包装/包。我在XML和滚动视图将其设置为FILL_PARENT / FILL_PARENT缩小到什么似乎是裹/包。它真的很奇怪。纵观foursquared Android源$ C ​​$ C我发现了一个注释,开发人员说,同样的事情:

If you're wondering why I am doing this at all and NOT just setting it in the XML its because I am seeing something weird where the layout "jumps" to a wrap/wrap. I AM setting it to fill_parent/fill_parent in the XML and on scroll the view "shrinks" to what appears to be wrap/wrap. Its really weird. Looking at the foursquared android source code I found a comment where the developer says the same thing:

一个奇怪的现象是怎么回事的布局参数虽然。如果我们没有明确设置布局中填充/充气后补,布局跳转到一个包裹/包的布局。 (FriendsActivity.java:258)

"Something odd is going on with the layout parameters though. If we don't explicitly set the layout to be fill/fill after inflating, the layout jumps to a wrap/wrap layout." (FriendsActivity.java:258)

这听起来就像我的问题,所以我试图做的(通过code组布局)相同,但在他的案件,他正在使用的LinearLayout,而我使用RelativeLayout的。

Which sounds just like my problem, hence I am trying to do the same (set layout via code), but in his case he is using LinearLayout, whereas I am using RelativeLayout.

我在想什么?先谢谢了。

What am I missing? Thanks in advance.

推荐答案

布局PARAMS必须是母公司的类型。所以,如果你正在创建一个RelativeLayout的并将其添加到ListView,布局PARAMS类型必须ListView.LayoutParams,不RelativeLayout.LayoutParams的。做正确的方法是调用夸大这种方式:

The layout params have to be of the type of the parent. So if you are creating a RelativeLayout and adding it to a ListView, the layout params must be of type ListView.LayoutParams, not RelativeLayout.LayoutParams. The correct way to do it is to call inflate this way:

膨胀(R.layout.mylayout,theListView,假);

inflate(R.layout.mylayout, theListView, false);

该版本充气的()将正确地充气的布局参数(安卓layout_ *)。在XML定义

This version of inflate() will correctly inflate the layout parameters (android:layout_*) defined in XML.

 
精彩推荐
图片推荐