0

我用这个代码导航抽屉安卓热门NavigationView内容不见了

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

鉴于Android的工作室工作正确: 但在设备后http://i65.tinypic.com/wtvu4k.jpg

发射,菜单顶部已经消失: http://i68.tinypic.com/2nu0ub5.jpg

下一个问题是:点击不是菜单操作

请帮我

回答

1

#问题1:顶部菜单的已经没有了。

添加android:paddingTop="24dp"menu容器RelativeLayout,显示top菜单TextView

2.添加android:background="?attr/selectableItemBackground"TextView显示就可以了ripple effectclick

更新您的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" 
    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> 

仅供参考,我已经加入android:padding="16dp"所有的TextView的给予适当的缩进。

#问题2:点击不可用于菜单。

设置onClick监听到TextView处理click事件:

// 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(); 
     } 
    }); 
} 

OUTPUT:

enter image description here

希望这将有助于〜

1

你不应该添加菜单布局:在navigationView你可以添加菜单directly..using app:menu="@menu/menu_navigation"

使用此布局:

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

在res /菜单/ menu_navigation像添加菜单项:

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

下面是完整的Example

+0

我不使用菜单,组和项目。我想要使​​用线性布局。我可以吗 ? –

+0

看到这个:http://stackoverflow.com/questions/30626324/navigationview-and-custom-layout ..它可能有助于 – rafsanahmad007

+0

我检查它。这个答案会在