2017-03-31 87 views
-2

以下是創建導航抽屜的一些代碼。問題在於抽屜顯示不正確。抽屜在活動開始時是可見的,但僅部分可見,並且不會從側面滑入和滑出。它只是固定在一個地方。這裏有什麼問題?爲什麼導航抽屜的此代碼不起作用?

注:我在styles.xml

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: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="com.example.nirvan.navigationexample3.MainActivity"> 


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"></LinearLayout> 

    < 
    android.support.design.widget.NavigationView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:id="@+id/nv" 
    > 


    <ListView 
     android:layout_width="200dp" 
     android:layout_height="match_parent" 
     android:id="@+id/listView"></ListView> 

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

NoActionBar mainActivity.java

package com.example.nirvan.navigationexample3; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.support.v7.widget.Toolbar; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class MainActivity extends AppCompatActivity 
{ 

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


    ListView ls=(ListView)findViewById(R.id.listView); 
    String[] values= 
    {"one","two","three","four","five","six","seven","eight"}; 


    ArrayAdapter<String> adapter=new ArrayAdapter(this, 
    R.layout.item,values); 
    ls.setAdapter(adapter); 
    } 
} 
0刪除的操作欄

item.xml

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

</TextView> 
+0

您在抽屜「視圖」中缺少適當的'layout_gravity'屬性。 –

回答

0

編輯您的佈局文件是這樣的:

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

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

     <include layout="@layout/nav_header_main" 
        android:id="@+id/my"/> 
       <ListView 

        android:layout_weight="7" 
        android:layout_width="match_parent" 
        android:layout_height="0dp" 
        android:id="@+id/list_view_inside_nav"></ListView> 
      </LinearLayout> 
     </android.support.design.widget.NavigationView> 

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

但是爲什麼'LinearLayout'應該包含'Navigation View'?我的理解是,第一個'LinearLayout'是當抽屜被隱藏的時候。 「導航視圖」包含抽屜的內容。那麼爲什麼'LinearLayout'包含視圖? – Nirvan

+0

如果你想使用列表視圖來顯示抽屜菜單中的選項,你可以使用上面的佈局(編輯) –

+0

那麼剩餘的應用程序的XML代碼去哪裏?我正在談論抽屜隱藏時顯示的部分。 – Nirvan

0

除了路易斯·費爾南多的答覆,不要忘了把

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

在XML文件的結尾。