无法为显示 class 转换异常的包含文件中存在的相对布局动态设置布局参数

Cannot dynamically set Layout parameters for Relative layout present inside include file showing class cast exception

     <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/parentLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="">
    <include android:id="@+id/contentLayout" layout="@layout/content_our_story" />
</android.support.design.widget.CoordinatorLayout>

content_our_story.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=""
    android:layout_margin="5dp"
    tools:showIn="@layout/activity_our_story">


</RelativeLayout>

设置布局参数代码:

contentLayout = (RelativeLayout) findViewById(R.id.contentLayout);
  RelativeLayout.LayoutParams param=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        param.setMargins(0,0,0,0);
        contentLayout.setLayoutParams(param);

当我尝试使用 "contentLayout" 的 ID 设置 LayoutParams 时,它显示 class 强制转换异常并期望它是一个协调器布局。但是,它包含如图所示的相对布局 above.Do 建议。

您需要使用要将参数设置为的视图的父布局的 LayoutParams:

CoordinatorLayout.LayoutParams param=new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT);