以下是创建导航抽屉的一些代码。问题是抽屉没有正确显示。从活动开始时可以看到抽屉,但只是部分抽屉,它不会从侧面滑入和滑出。它只是在一个地方静止不动。这有什么不对?
注意:我已在NoActionBar
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>
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);
}
}
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 :(得分: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>
答案 1 :(得分:0)
除了Luiz Fernando的回复,别忘了把
</android.support.v4.widget.DrawerLayout>
在XML文件的末尾。