2017-08-25 60 views
-1

我想在具有自定义视图的活动上添加片段。使用框架布局中的片段加载自定义视图

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> 

请帮我!!!!! :-(

+0

添加MainContentFrameLayout xml文件 –

+0

添加@Nabin Khatiwada – Tejas

回答

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>