簡體   English   中英

頂部ActionBar上的Android導航抽屜

[英]Android Navigation Drawer on top ActionBar

我正試圖將導航抽屜放在操作欄上,當它向右滑動時就像這個應用程序:[已刪除]

這是我的主要活動的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
    <RelativeLayout android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">
        ...
    </RelativeLayout>
    <fragment android:name="com...." 
        android:layout_gravity="start" 
        android:id="@id/navigation" 
        android:layout_width="@dimen/navigation_menu_width" 
        android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>

stackoverflow上的其他一些問題與此問題類似,但所有答案都建議使用滑動菜單庫。 但是這個應用程序他們仍然使用android.support.v4.widget.DrawerLayout並且他們成功了。 不要問我怎么知道他們使用標准導航抽屜,但我確定。

非常感謝您的幫助。


這是最終的解決方案 :非常感謝@Peter Cai這項工作完美無缺。 https://github.com/lemycanh/DrawerOnTopActionBar

屏幕截圖半透明的KitKat

我從https://github.com/jfeinstein10/SlidingMenu學到了一個小技巧,以實現您所需的效果。

您只需要刪除窗口裝飾視圖的第一個子項,並將第一個子項添加到抽屜的內容視圖中。 之后,您只需將抽屜添加到窗口的裝飾視圖中。

以下是您執行此操作的一些詳細步驟。

首先,創建一個名為“decor.xml”的xml或任何你喜歡的東西。 只放入DrawerLayout和抽屜。下面的“FrameLayout”只是一個容器。 我們將用它來包裝您活動的內容。

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout ...>
    <FrameLayout android:id="@+id/container"
        android:orientation="vertical" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"/>
    <fragment android:name="com...." 
        android:layout_gravity="start" 
        android:id="@id/navigation" 
        android:layout_width="@dimen/navigation_menu_width" 
        android:layout_height="fill_parent" />
</android.support.v4.widget.DrawerLayout>

然后刪除主布局中的DrawerLayout。 現在主要活動的布局應該是這樣的

<RelativeLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    ...
</RelativeLayout>

我們假設主要活動的布局名為“main.xml”。

在您的MainActivity中,寫如下:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Inflate the "decor.xml"
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    DrawerLayout drawer = (DrawerLayout) inflater.inflate(R.layout.decor, null); // "null" is important.

    // HACK: "steal" the first child of decor view
    ViewGroup decor = (ViewGroup) getWindow().getDecorView();
    View child = decor.getChildAt(0);
    decor.removeView(child);
    FrameLayout container = (FrameLayout) drawer.findViewById(R.id.container); // This is the container we defined just now.
    container.addView(child);

    // Make the drawer replace the first child
    decor.addView(drawer);

    // Do what you want to do.......

}

現在你有一個可以在ActionBar上滑動的DrawerLayout。 但您可能會發現狀態欄覆蓋了它。 您可能需要向Drawer添加paddingTop才能解決問題。

更新:如何使用導航抽屜覆蓋操作欄。 (使用新工具欄)在build.gradle中的依賴項中使用這些

compile 'com.android.support:appcompat-v7:21.0.0'
compile 'com.android.support:support-v4:21.0.0'

這是您的抽屜布局

<!-- 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">
    <LinearLayout
        android:id="@+id/layout_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    <include layout="@layout/toolbar"/>
    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"/>

    </LinearLayout>
    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@color/list_background"
         />

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

在布局文件夾中創建新的toolbar.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_height="wrap_content"
    android:layout_width="match_parent"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

轉到擴展導航抽屜的活動。 並在SetContentView()之后添加它

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

不要忘記在您的values文件夾中擴展主題NoActionBar。

<style name="Theme.Whtsnxt" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="windowActionBar">false</item>
    <!-- colorPrimary is used for the default action bar background -->
    <item name="windowActionModeOverlay">true</item>
    <item name="android:textColorPrimary">@color/white</item>
    <item name="colorPrimary">@color/splashscreen</item>
    <item name="colorPrimaryDark">@color/holo_blue_light</item>

    <item name="android:windowBackground">@color/white</item>
    <item name="android:colorBackground">@color/white</item>

</style>

我發布了一個技巧,可以在以前的Android L版本中實現這一點。 你可以在這篇文章中找到我的解決方案。 希望它對某人有用。

如果你不想使用lib或者這個hack:

  1. 擴展“Theme.AppCompat.NoActionBar”
  2. 將DrawerLayout的第一個元素移動到LinearLayout中。
  3. 將工具欄添加到此LinearLayout。

     <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:minHeight="?attr/actionBarSize" android:layout_width="match_parent" android:layout_height="wrap_content" app:titleTextColor="@android:color/white" android:background="?attr/colorPrimary"> </android.support.v7.widget.Toolbar> 
  4. 在Activity中在setContentView之后添加以下行

     setSupportActionBar((Toolbar) findViewById(R.id.toolbar)); 
  5. 現在它應該工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM