1

hinzufügen Ich habe eine Navigationsleiste erstellt, wenn Benutzer auf hamburger Symbol klicken, öffnet es die Schublade. Ich muss eine Schließen-Schaltfläche in drawer hinzufügen. Das muss ich umsetzen.Wie Cross-Button in der oberen rechten Ecke der Navigationsleiste

enter image description here

ich versucht habe, bunt nicht in der Lage, ein Bild auf den Drawer Layout hinzuzufügen. Dies ist mein Code, um Navigation Drawer hinzuzufügen. Bitte führen Sie mich, wie Sie ein Bild in der oberen rechten Ecke der Schublade hinzufügen.

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:id="@+id/base_layout"> 

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


</LinearLayout> 


    <android.support.design.widget.NavigationView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:id="@+id/navigation_view" 
     android:layout_gravity="start" 
     android:background="@drawable/sidebg" 
     app:menu="@menu/drawer_menu" 
     app:itemTextColor= "#ffffff" 
     app:headerLayout="@layout/navigation_drawer_header" 
     android:gravity="bottom|left" 
     android:dividerHeight="0dp" 
     app:itemIconTint="@android:color/white" 
     > 

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




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

Antwort

0

einen Klick Zuhörer auf die Imageview des X-Symbol hinzufügen.

imageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Drawer.closeDrawer(Gravity.LEFT); 
     } 
    }); 

das Symbol hinzuzufügen können, eine Art benutzerdefinierte Ansicht

<android.support.v4.widget.DrawerLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="android-coding.blogspot.com" /> 
</RelativeLayout> 

<LinearLayout 
    android:id="@+id/drawer" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:orientation="vertical" 
    android:background="@android:color/background_dark" 
    android:padding="5dp" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="android-coding"/> 
    <ImageView 
     android:id="@+id/imageview" 
    android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="yourdrawable" 
     /> 
</LinearLayout> 

tun wollen Siehe My source

Same as your question.

+0

aber wie Bildansicht in der Navigationsschublade hinzufügen – Kirmani88

+0

Ah ja siehe die Updates. Es gibt wahrscheinlich andere Möglichkeiten, benutzerdefinierte Ansichten zu erstellen. Das ist das Schlüsselwort, das du brauchst. – StarWind0

1

ActionBarDrawerToggle Ihr Problem beheben:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description */ 
       R.string.drawer_close /* "close drawer" description */ 
       ) { 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       super.onDrawerClosed(view); 
       getActionBar().setTitle(mTitle); 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       getActionBar().setTitle(mDrawerTitle); 
      } 
     }; 

     // Set the drawer toggle as the DrawerListener 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 

Alternative Art und Weise:

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

    <android.support.v4.widget.DrawerLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 


     <!--your layout here --> 
    </android.support.v4.widget.DrawerLayout> 


    <ImageView 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:id="@+id/im_close_btn" 
     android:layout_width="48dp" 
     android:layout_height="48dp" 
     android:visibility="gone" 
     android:src="@drawable/ic_close"/> 
</RelativeLayout> 
+0

Danke, aber ich kann immer noch nicht verstehen, wie man 'X' Bild in der oberen rechten Ecke hinzufügt – Kirmani88

+0

Wenn Sie ActionBarDrawerToggle verwenden möchten, machen Sie es RTL wird Ihr Problem beheben. Alternativ können Sie einen Schließen-Button in die Ecke stellen und wenn Drawer Open sichtbar machen. – Amir

+0

Danke, lassen Sie mich den alternativen Ansatz versuchen – Kirmani88

0
navigation_drawer_header.xml 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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="@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"> 
    <Button 
     android:id="@+id/closeButton" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="X" 
     android:layout_gravity="right"/> 
    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     app:srcCompat="@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> 

Java-Code

View headerView = navigationView.getHeaderView(0); 
     Button closeButton = (Button) headerView.findViewById(R.id.closeButton); 

     closeButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       drawer.closeDrawer(Gravity.LEFT); 
      } 
     }); 
+0

@ Kirmani88 versuche diese Antwort ... –

0

Ich glaube, Sie wollen wissen, wie die Schaltfläche "x" hinzuzufügen. Nun, sehen Sie die folgende Zeile in Ihrem NavigationView:

Dies ist das Header-Layout Ihrer Schublade. Öffne dieses Layout und lege dein ImageView hinein. So einfach ist das.

Verwandte Themen