设置PARAMS一个RelativeLayout的内部充气的观点,但越来越ClassCastException异常 [英] Setting params for an inflated view inside a RelativeLayout, but getting classcastexception

查看:195
本文介绍了设置PARAMS一个RelativeLayout的内部充气的观点,但越来越ClassCastException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想补充的布局以编程方式使用充气(这是一个简单的TextView)。充气视图的父是一个RelativeLayout的是一个滚动型。我的问题是,我试图把它的标题下的新视图使用params.addRule和view.setLayoutParams()(这是在RelativeLayout的),但我得到一个classcast异常: 11 -12 13:06:57.360:E / AndroidRuntime(10965):java.lang.ClassCastException:android.widget.RelativeLayout $的LayoutParams不能转换为android.widget.FrameLayout $的LayoutParams ,即使视图的父母显然是一个RelativeLayout的。有什么想法?

我想那是因为RelativeLayout的嵌套在一个滚动视图,但我不明白为什么它会做到这一点,因为我加入是一个RelativeLayout的视图的直接父。

  _innerLayout =(RelativeLayout的)findViewById(R.id.innerRelativeLayout);
    LayoutInflater吹气=(LayoutInflater)this.getLayoutInflater();    查看查看= inflater.inflate(R.layout.comments,_innerLayout);
    RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW,R.id.commentsHeader);
    view.setLayoutParams(PARAMS);


解决方案

如果你使用一个非空查看作为与膨胀根( )方法查看返回实际上将查看作为方法根传递。所以,你的情况视图 _innerLayout RelativeLayout的这有作为父项的滚动型,这是一个扩展的FrameLayout (这将解释的LayoutParams )。

因此​​,无论使用当前充气的方法,但查找视图在返回的视图:

 查看= inflater.inflate(R.layout.comments,_innerLayout);
RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.commentsHeader);
(view.findViewByid(R.id.theIdOfTextView))setLayoutParams(PARAMS)。

或使用的另一个版本膨胀()(不重视他虚增观点和手动添加它连同适当的的LayoutParams

 查看= inflater.inflate(R.layout.comments,_innerLayout,FALSE);
RelativeLayout.LayoutParams PARAMS =新RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW,R.id.commentsHeader);
view.setLayoutParams(PARAMS);
_innerLayout.addView(视图);

Hi i'm trying to add a layout programmatically using the inflater (it's a simple TextView). The parent of the inflated view is a RelativeLayout which is in a ScrollView. My problem is that I'm trying to place the new view under its header (which is in the RelativeLayout) using params.addRule and view.setLayoutParams(), but I'm getting a classcast exception: 11-12 13:06:57.360: E/AndroidRuntime(10965): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams, even though the parent of the view is clearly a RelativeLayout. Any thoughts?

I'm thinking it's because the RelativeLayout is nested in a scrollview, but I don't understand why it would do this since the direct parent of the view I'm adding is a RelativeLayout.

    _innerLayout = (RelativeLayout) findViewById(R.id.innerRelativeLayout);
    LayoutInflater inflater = (LayoutInflater)this.getLayoutInflater();

    View view = inflater.inflate(R.layout.comments, _innerLayout);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, R.id.commentsHeader);
    view.setLayoutParams(params);

解决方案

If you use a non null View as the root with the inflate() method the View returned will actually be the View passed as the root in the method. So, in your case view is the _innerLayout RelativeLayout which has as a parent a ScrollView, which is an extension of FrameLayout(which will explain the LayoutParams).

So either use the current inflate method, but look for the view in the returned view:

View view = inflater.inflate(R.layout.comments, _innerLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.commentsHeader);
(view.findViewByid(R.id.theIdOfTextView)).setLayoutParams(params);

or use another version of inflate()(which doesn't attach he inflated view and manually add it along with proper LayoutParams):

View view = inflater.inflate(R.layout.comments, _innerLayout, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.commentsHeader);
view.setLayoutParams(params);
_innerLayout.addView(view);

这篇关于设置PARAMS一个RelativeLayout的内部充气的观点,但越来越ClassCastException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆