1
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<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_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".main_drawer">

    <android.support.v7.widget.Toolbar
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/toolbar"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:fitsSystemWindows="true"
         android:background="@color/octo"/>

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    </FrameLayout>

    <!-- android:layout_gravity="start" tells DrawerLayout to treat
         this as a sliding drawer on the left side for left-to-right
         languages and on the right side for right-to-left languages.
         If you're not building against API 17 or higher, use
         android:layout_gravity="left" instead. -->
    <!-- The drawer is given a fixed width in dp and extends the full height of
         the container. -->
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="am.octogr.app.octocopy.NavigationDrawerFragment"

        tools:layout="@layout/main_drawer_listview" />


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

こんにちは、このレイアウト内にツールバーを配置するにはどうすればよいですか? このナビゲーション ドロワーのレイアウト。いくつかのコードを試しましたが、まだ機能していません。1. FrameLayout: ツールバーが表示されませんでした。2. FrameLayout の下: ツールバーは表示されますが、コンテンツは表示されません。3. LinearLayout に DrawerLayout がありますが、私のフラグメントはうまく機能していないようです。前もって感謝します。

4

5 に答える 5

1

説明してみましょう。

Android はViews を上から下に描画します。A - B - C - あなたの には 3 つのそれぞれのビューがありますDrawerLayout。現在、AToolbarは最初にwidth全体の幅にheight合わせてコンテンツ自体に合わせて描画され、B はwidth全体の幅に合わせて全体の高さに合わせて描画されheightます。つまり、FrameLayout は描画の連続で2番目に描画され、あなたの上に表示されますToolbarが、覆われているため、そこにあることがわかりません。下に持ってくると、ツールバーが2番目に連続して描画されるため、表示されます。

今から解決策を差し引いていただければ幸いです。

于 2015-07-04T01:13:56.807 に答える
0

これを試して :

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".main_drawer">

    <LinearLayout
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:orientation="vertical"
    >

    <include
        android:id="@+id/toolbar"
        layout="@layout/tool_bar"
    />

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

    </FrameLayout>

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="am.octogr.app.octocopy.NavigationDrawerFragment"

        tools:layout="@layout/main_drawer_listview" />

        <!-- Your Content -->

</LinearLayout>

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

別のtool_bar.xmlで:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:background="@color/octo"
        android:elevation="6dp"
        android:minHeight="?attr/actionBarSize">
</android.support.v7.widget.Toolbar>

ツールバーを初期化し、ActionBar として設定することを忘れないでください。

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

minSdkVersion が 14の場合

于 2015-07-04T01:26:00.717 に答える
0

メイン コンテンツ ビューとなる別のレイアウトにツールバーを配置する

 <!-- The main content view -->
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            >
    <android.support.v7.widget.Toolbar
             xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@+id/toolbar"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:fitsSystemWindows="true"
             android:background="@color/octo"/>

        <!-- As the main content view, the view below consumes the entire
             space available using match_parent in both dimensions. -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

        </FrameLayout>
        </LinearLayout>
于 2015-07-04T00:57:46.693 に答える
0

これを試して。それは私にとってはうまくいきます。

この XML をドロワー アクティビティに使用します。

<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_layout" />

    <!-- The navigation drawer -->
    <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_drawer"
        app:menu="@menu/activity_drawer" />

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

app_bar_layout.xml

<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.rsa.myapplication.DrawerActivity">

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

        <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/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/> <!--This line is important-->

</android.support.design.widget.CoordinatorLayout>
于 2016-04-19T10:08:24.533 に答える