工具栏与内容视图重叠

时间:2018-04-09 11:59:03

标签: android android-layout

我正在使用“?attr / actionBarSize”让TextView显示在App Bar下方。我知道actionBarSize高度是56dp,但我认为应该添加小状态栏的高度。是否有可用的属性,以便我不必以编程方式执行此操作,而是使用XML格式?

activity_some.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?attr/actionBarSize"
        android:background="@color/colorAccent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:textSize="40sp" />

    </LinearLayout>

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

</FrameLayout>

drawer_layout_include:

<?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"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <include
        layout="@layout/header_tool_bar"
        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/header_navigation_drawer"
        app:menu="@menu/navigation_drawer" />

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

header_tool_bar.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"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <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>

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

这些文件只是Android Studio导航器项目中的普通文件。

enter image description here

1 个答案:

答案 0 :(得分:0)

您正在以令人困惑的方式实施抽屉菜单。实现抽屉的正确方法就是这样。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
    android:layout_height="match_parent"

        android:orientation="vertical">
        // your toolbar
        <include layout="@layout/layout_toolbar_default" />
// content to show below toolbar, it can be layout or fragments
        <include layout="@layout/activity_main_content" />

    </LinearLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:visibility="visible"
        android:fitsSystemWindows="false">

      ....

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

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

这就是抽屉布局,为什么需要编写复杂的代码。