在框架布局中使用片段加载自定义视图

Loading Custom View using Fragment inside a Frame layout

我正在尝试在 Activity 上添加一个具有自定义视图的片段。

 private  void loadMonitorPage(){
   fragmentManager = getFragmentManager();
   MonitorFragment monitorFragment = new MonitorFragment();
   fragmentTransaction = fragmentManager.beginTransaction(); 
   fragmentTransaction.replace(R.id.MainContentFrameLayout,monitorFragment);
   fragmentTransaction.addToBackStack(null);
   fragmentTransaction.commit();
}

这里的 MainContentFrameLayout 是我在第一张图片中提到的第二个块。

这是我的 Fragment 布局 xml(fragment_monitor)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorContentApp"
tools:context="fragments.MonitorFragment">

            <monitorView.GridView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorAccent"
                />
</LinearLayout>

而不是这个自定义视图覆盖全屏

这是我的自定义视图Class

public class GridView 扩展视图 {

private Paint mgridpaint;

private float mGridViewWidth;
private float mGridViewHeight;

private Canvas mGridCanvas;

public GridView(Context context) {
    super(context);
}

public GridView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public GridView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}


@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    // TODO: consider storing these as member variables to reduce
    // allocations per draw cycle.
    int paddingLeft = getPaddingLeft();
    int paddingTop = getPaddingTop();
    int paddingRight = getPaddingRight();
    int paddingBottom = getPaddingBottom();

    int contentWidth = getWidth() - paddingLeft - paddingRight;
    int contentHeight = getHeight() - paddingTop - paddingBottom;

    mgridpaint = new Paint();
    mgridpaint.setColor(Color.rgb(0,255,0));
    mGridCanvas = canvas;


    mGridViewWidth  = contentWidth;
    mGridViewHeight = contentHeight;

    //drawGrid();
 }

}

这是我的父布局。我要添加此 Fragment

的第二个 FrameLayout
    <LinearLayout 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="com.mactrical.mindoter.MainActivity"
    android:weightSum="10"
    android:orientation="vertical"
    tools:showIn="@layout/app_bar_main">

  <FrameLayout
    android:id="@+id/ECGDetailsFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.5"
    android:background="@color/colorHeaderApp">

  </FrameLayout>

  <FrameLayout
    android:id="@+id/MainContentFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="8">

  </FrameLayout>

  <FrameLayout
    android:id="@+id/ECGFooterFrameLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:background="@color/colorFooterApp">
  </FrameLayout>

</LinearLayout>

请帮帮我!!!!! :-(

我找到问题了 在这个布局中,我必须使用 0dp 作为我的高度而不是 wrap_content

<LinearLayout 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="com.mactrical.mindoter.MainActivity"
android:weightSum="10"
android:orientation="vertical"
tools:showIn="@layout/app_bar_main">

 <FrameLayout
   android:id="@+id/ECGDetailsFrameLayout"
   android:layout_width="match_parent"
   android:layout_height="0dp"
   android:layout_weight="1.5"
   android:background="@color/colorHeaderApp">

 </FrameLayout>

 <FrameLayout
  android:id="@+id/MainContentFrameLayout"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="8">

</FrameLayout>

<FrameLayout
  android:id="@+id/ECGFooterFrameLayout"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="0.5"
  android:background="@color/colorFooterApp">
</FrameLayout>

</LinearLayout>