2016-06-02 7 views
-1

Ich möchte unter die Navigationsleiste ähnlich der der LinkedIn-App replizieren:Wie können Sie einem Navigationsfach dauerhafte Schaltflächen hinzufügen?

enter image description here

Beachten Sie, dass die Nachricht, Benachrichtigung und die Einstellungen Schaltflächen am oberen Rand sind persistent und sind nicht Teil des scrollbaren Liste.

Ich habe das offizielle Android-Tutorial für Navigation Drawer sowie zahlreiche andere Tutorials verfolgt, die einfach kein zusätzliches Licht dafür liefern, wie ich das erreichen kann.

Das Beste, was ich mir vorstellen konnte, war, dem Header-Layout der Navigationsschubladen-Vorlage in Android Studio 2.1.1 3 ImageButtons hinzuzufügen.

EDIT: Wie Prats und ein paar andere Ratschläge, habe ich ein zusätzliches Header-Layout kurz vor der Schublade, die die 3 Tasten enthält, aber egal die Z-Reihenfolge der Layouts innerhalb der XML, es scheint sich hinter der Schublade zu verstecken. Siehe Bild unten stehende Abbildung:

enter image description here enter image description here

Im Folgenden sind die Layout-Dateien:

header.xml (enthält die 3 Tasten)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="right" 
    android:background="#111"> 

    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:src="@drawable/ic_menu_manage" /> 

    <ImageButton 
     android:id="@+id/imageButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:src="@drawable/ic_menu_manage" /> 

    <ImageButton 
     android:id="@+id/imageButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:src="@drawable/ic_menu_manage" /> 
</LinearLayout> 

nav_header_main.xml (default erzeugt Header)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="right" 
    android:background="#111"> 

    <ImageButton 
     android:id="@+id/imageButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:src="@drawable/ic_menu_manage" /> 

    <ImageButton 
     android:id="@+id/imageButton3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:src="@drawable/ic_menu_manage" /> 

    <ImageButton 
     android:id="@+id/imageButton2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal|center_vertical" 
     android:src="@drawable/ic_menu_manage" /> 
</LinearLayout> 

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="#fff"> 


     <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="false" 
      app:headerLayout="@layout/nav_header_main" 
      app:menu="@menu/activity_main_drawer" /> 

     <include 
      android:id="@+id/header" 
      layout="@layout/header" 
      android:layout_width="fill_parent" 
      android:layout_height="50dp" 
      android:layout_alignParentTop="true" /> 

    </RelativeLayout> 
</android.support.v4.widget.DrawerLayout> 
+0

Try .In diesem können Sie diese Tasten in headerlayout definieren –

+0

Setzen Sie Ihre 'Button' oberhalb der 'NavigationView' in eine andere' ViewGroup', die Sie als die Schublade verwenden. –

Antwort

1

versuchen, wie dies zu tun:

header.xml (oberste Layout, in dem Sie drei Bilder haben)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal"> 

     <ImageButton 
      android:id="@+id/imageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal|center_vertical" 
      android:src="@drawable/ic_menu_manage" /> 

     <ImageButton 
      android:id="@+id/imageButton3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal|center_vertical" 
      android:src="@drawable/ic_menu_manage" /> 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_horizontal|center_vertical" 
      android:src="@drawable/ic_menu_manage" /> 
    </LinearLayout> 

</LinearLayout> 

nav_header_main (Standard android-Header)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:src="@android:drawable/sym_def_app_icon" /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="Android Studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 
    <TextView 
     android:id="@+id/textView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="[email protected]" /> 
</LinearLayout> 

dann schließlich das Hauptlayout zeigt

  1. Header (oben)
  2. nav_main_header
  3. Listenansicht

<!-- The main content view --> 
<FrameLayout 
    android:id="@+id/flFragmentContainer" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<!-- The navigation drawer --> 
<RelativeLayout 
    android:layout_width="240dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#111"> 

    <include android:id="@+id/header" 
     layout="@layout/header" 
     android:layout_width="fill_parent" 
     android:layout_alignParentTop="true" 
     android:layout_height="100dp"/> 

    <include android:id="@+id/header2" 
     layout="@layout/nav_header_main" 
     android:layout_width="fill_parent" 
     android:layout_below="@id/header" 
     android:layout_height="100dp"/> 

    <ListView 
     android:id="@+id/lvDrawer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:choiceMode="singleChoice" 
     android:layout_below="@id/header2" 
     android:divider="@android:color/transparent" 
     android:dividerHeight="0dp" /> 
</RelativeLayout> 

+0

Danke, das ist ein gutes Sprungbrett, aber während ich versucht habe, zu tun, was Sie empfehlen, scheint die Tasten immer hinter dem Schubladen-Layout zu sein. Bitte beachten Sie, dass ich die neueste Vorlage in Android Studio 2.1 verwende, die NavigationView als Schublade verwendet. Ich habe die Frage aktualisiert, um diese Schwierigkeit widerzuspiegeln. Hoffentlich könntest du die Antwort wissen ... – chaser

+0

Mit dem relativen Layout herumspielen, konnte ich dieses Problem beheben, danke, ich werde dies als gelöst aktualisieren :) – chaser

0

Verwenden recyclerview mit Adapter dafür. Sie können eine beliebige Struktur von benutzerdefinierten Ansichten für Elemente implementieren.

Verwandte Themen