2015-07-03 121 views
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,但我的片段看起來效果不好。 預先感謝您。<Android>工具欄不顯示

回答

1

讓我試着解釋。

Android從上到下依次繪製View; A - B - C - 您是否在您的DrawerLayout中有3種不同的意見。現在A是Toolbar首先用參數width繪製出適合整體寬度和height適合內容本身,B用width適合整體寬度和height適合整體高度等繪製。這意味着您的FrameLayout會在繪圖序列中以及您的Toolbar之上繪製第二個,但是由於它覆蓋後,您看不到它在那裏,所以如果您將它放在下面,則會連續繪製您的工具欄,因此您可以看到它。

我希望你現在可以從中扣除解決方案。

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

不要忘記初始化工具欄,並將其設置爲動作條:

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

With a minSdkVersion of 14

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>