导航抽屉碎片在顶部切断

时间:2016-08-20 03:14:03

标签: android android-fragments android-navigation-drawer

我在Android上使用导航抽屉模板,我也在使用片段。每当有片段时,该动作都会切断片段的顶部。除了添加片段之外,我还没有更改默认代码。如何在操作栏下方加载片段?

问题图片:

*更新 现在看起来像这样 image

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<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:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

片段xml

<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"
tools:context="com.example.android.slidemenu.TestFragment"
android:orientation="horizontal">

<!-- TODO: Update blank fragment layout -->
<ImageView
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@android:drawable/ic_menu_compass"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TESTTEST"
    android:textSize="50sp"/>
</LinearLayout>

app bar main xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.example.android.slidemenu.MainActivity">

<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/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

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

3 个答案:

答案 0 :(得分:2)

FrameLayout需要低于Toolbar,并且在CoordinatorLayout内。试着像这样重组。

包括&#34;主要内容&#34;在DrawerLayout。然后在FrameLayout之后添加AppBarLayout(如果不需要片段,则包含其他视图)。

&#34;推动&#34;在FrameLayout下面是这一行

app:layout_behavior="@string/appbar_scrolling_view_behavior"

抽屉布局XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 
    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:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <include
        layout="@layout/nav_content"/>

    <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:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

nav_content.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    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"
    android:fitsSystemWindows="true"
    tools:context="com.example.android.slidemenu.MainActivity">

    <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/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

答案 1 :(得分:0)

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } NotificationManager nmanger; static int id=1; public void buclick(View view) { NotificationCompat.Builder nbuild= (NotificationCompat.Builder) new NotificationCompat.Builder(this) .setContentTitle("Danger") .setContentText("the raining is comming soon") .setSmallIcon(R.drawable.amule); nmanger =(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); nmanger.notify(id,nbuild.build()); id++; } 放在FrameLayout之后的CoordinatorLayout内。

编辑:尝试删除AppBarLayout

答案 2 :(得分:0)

android:fitsSystemWindows 设置为 false 而不是删除它对我来说很有效。 Thx amitairos 提示;).