2016-08-24 73 views
0

我正在尝试创建导航视图片段。我创造了这个。有用。但它没有显示出精确的。我有2个XML代码。导航视图布局片段不显示

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 


</android.support.design.widget.AppBarLayout> 

<FrameLayout 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

</FrameLayout> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    app:srcCompat="@android:drawable/ic_dialog_email" /> 

我的第二个XML代码

<?xml version="1.0" encoding="utf-8"?> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:theme="@style/AppTheme.AppBarOverlay"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 
    <FrameLayout 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

    </FrameLayout> 

</android.support.design.widget.AppBarLayout> 


<android.support.design.widget.FloatingActionButton 
    android:id="@+id/fab" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom|end" 
    android:layout_margin="@dimen/fab_margin" 
    app:srcCompat="@android:drawable/ic_dialog_email" /> 

enter image description here

如果我使用第一代码它给出了在左侧的图像的输出。如果我使用第二个代码,它会在右侧的图片中给出输出。我怎么解决这个问题 ?非常感谢。

enter image description here

fragment_main.xml

<FrameLayout 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" 
tools:context="com.example.hp.myapplication.MainFragment"> 

<!-- TODO: Update blank fragment layout --> 

<Button 
    android:text="Burası Ana Sayfa Fragmenti" 
    android:layout_width="match_parent" 
    android:layout_height="61dp" 
    android:id="@+id/button" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="61dp" 
    android:text="@string/hello_blank_fragment" /> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 

<include 
    layout="@layout/app_bar_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 


<android.support.design.widget.NavigationView 
    android:id="@+id/nav_view" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:fitsSystemWindows="true" 
    app:headerLayout="@layout/nav_header_main" 
    app:menu="@menu/activity_main_drawer" /> 

+0

您可以发布什么是预期的结果,也是完整的XML文件将帮助? –

+0

我想创建右侧。但它显示在工具栏区域?不是吗? –

+0

如果我做左边它不显示喜欢右边。 –

回答

0

您的framgent容器位于工具栏下方,并且取得完整的屏幕宽度和全高。

要使片段中心的内容垂直您可以使用

<FrameLayout 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:gravity="center_vertical" 
    tools:context="com.example.hp.myapplication.MainFragment"> 

    <!-- TODO: Update blank fragment layout --> 

    <Button 
    android:text="Burası Ana Sayfa Fragmenti" 
    android:layout_width="match_parent" 
    android:layout_height="61dp" 
    android:id="@+id/button" /> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="61dp" 
    android:text="@string/hello_blank_fragment" /> 

</Framelayout>