1
     <?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 时,它会显示类转换异常并期望它是协调器布局。但是,它包含如上所示的相对布局。请建议。

4

1 回答 1

1

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

CoordinatorLayout.LayoutParams param=new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.MATCH_PARENT, CoordinatorLayout.LayoutParams.MATCH_PARENT);
于 2016-07-20T11:24:11.420 回答