在Frame布局中使用Fragment加载自定义视图

时间:2017-08-25 10:28:07

标签: android android-layout android-fragments android-framelayout

我正在尝试在具有自定义视图的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是我在第一张图片中提到的第二个块。

enter image description here

这是我的片段布局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>

而不是此自定义视图覆盖整个屏幕 enter image description here

这是我的CustomView类

公共类GridView扩展了View {

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();
 }

}

这是我的父布局。第二帧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>

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

1 个答案:

答案 0 :(得分:0)

我弄明白了这个问题 在这个布局中,我不得不使用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>