0

J'utilise ce code pour tiroir de navigationAndroid: Top contenu NavigationView allé

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

     <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" 
      android:visibility="visible"> 
      <include 
       layout="@layout/menu" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" /> 

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

menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    tools:context="ir.app.ir.live24.MainActivity" 
    android:clickable="false" 
    android:gravity="center" 
    android:background="#fff"> 

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

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:id="@+id/textView3" 
      android:textColor="#000" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:id="@+id/textView4" 
      android:textColor="#000" 
      android:layout_weight="0.5" /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="New Text" 
      android:id="@+id/textView5" 
      android:textColor="#000" /> 
    </LinearLayout> 
</RelativeLayout> 

En vue studio android travail correct: http://i65.tinypic.com/wtvu4k.jpg

Mais dans l'appareil après lancement, le haut du menu est parti: http://i68.tinypic.com/2nu0ub5.jpg

problème suivant: cliquables ne fonctionne pour le menu

S'il vous plaît aider moi

Répondre

1

# Problème 1: Haut de menu a disparu.

1. Ajouter android:paddingTop="24dp"-menu conteneur RelativeLayout pour afficher le menu topTextView.

2. Ajouter android:background="?attr/selectableItemBackground"-TextView pour montrer la ripple effect quand click sur elle.

Mettez à jour votre menu.xml comme ci-dessous:

<?xml version="1.0" encoding="utf-8"?> 
<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" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:clickable="false" 
    android:gravity="center" 
    android:paddingTop="24dp" 
    android:background="#fff"> 

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

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="16dp" 
      android:text="New Text" 
      android:textColor="#000" 
      android:background="?attr/selectableItemBackground"/> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="16dp" 
      android:text="New Text" 
      android:textColor="#000" 
      android:layout_weight="0.5" /> 

     <TextView 
      android:id="@+id/textView5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="16dp" 
      android:text="New Text" 
      android:textColor="#000" 
      android:background="?attr/selectableItemBackground"/> 
    </LinearLayout> 
</RelativeLayout> 

Pour votre information, j'ai ajouté android:padding="16dp" à tous pour donner indentations appropriés de TextView.

# Problème 2: Cliquable ne fonctionne pas pour le menu.

Set onClick auditeur TextView pour gérer click événements:

// From Activity onCreate() method 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    ........................ 
    ............................... 

    TextView textView3 = (TextView) findViewById(R.id.textView3); 

    textView3.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Toast.makeText(getApplicationContext(), "Clicked", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

SORTIE:

enter image description here

Espérons que cela aidera ~

1

Vous ne devez pas ajouter la mise en page du menu: en navigationView vous pouvez ajouter le menu directly..using app:menu="@menu/menu_navigation"

utilisent cette disposition:

<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/menu" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     app:menu="@menu/menu_navigation" //menu items in drawer 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     android:visibility="visible"></android.support.design.widget.NavigationView> 

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

maintenant dans res/menu/menu_navigation ajouter les éléments de menu comme:

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/profile" 
      android:icon="@drawable/profile_icon" 
      android:title="Profile" /> 

     <item 
      android:id="@+id/settings" 
      android:icon="@drawable/settings_icon" 
      android:title="Settings" /> 

     <item 
      android:id="@+id/about_us" 
      android:icon="@drawable/about_us" 
      android:title="About us" /> 

    </group> 
</menu> 

Voici la complète Example

+0

Je n'utiliser le menu, groupe et article. Je veux utiliser la disposition linéaire. puis-je ? –

+0

voir ceci: http://stackoverflow.com/questions/30626324/navigationview-and-custom-layout ..cela peut aider – rafsanahmad007

+0

je le vérifie. cette réponse ajoute une disposition linéaire dans