我已经看了很多解决方案,但我的NavigationView出现了问题。我创建它并尝试使用它但不知何故它没有反应。这是我使用的代码。 谢谢 !
这是我的" drawermenu"
的XML <?xml version="1.0" encoding="utf-8"?>
<item
android:id="@+id/add"
android:icon="@drawable/ic_add_circle_black_24px"
android:title="Добави автомобил" />
<item
android:id="@+id/settings"
android:icon="@drawable/ic_build_black_24px"
android:title="Настройки" />
<item
android:id="@+id/export"
android:icon="@drawable/ic_importexport"
android:title="Експорт на инф."
/>
<item
android:id="@+id/about"
android:icon="@drawable/ic_info_outline_black_24px"
android:title="За програмиста" />
<item
android:id="@+id/exit"
android:icon="@drawable/ic_exit_to_app_black_24px"
android:iconTintMode="src_in"
android:title="Изход" />
</group>
这就是我用于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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="@color/myYellow"
android:paddingRight="10dp"
android:theme="@style/AppTheme2"
tools:context="com.example.hpdemon.diploma.MainForm"
tools:openDrawer="start">
<FrameLayout
android:id="@+id/flcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nv"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/myYellow"
android:fitsSystemWindows="true"
app:headerLayout="@layout/header"
app:itemIconTint="@color/colorPrimaryDark"
app:itemTextColor="#56733b"
app:menu="@menu/drawermenu">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是MainActivity的代码。我在onCreate方法中调用了Drawer函数
public void Drawer() {
NavigationView navigationView = findViewById(R.id.nv);
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
// close drawer when item is tapped
int id = menuItem.getItemId();
if (id == R.id.add) {
//Handle your stuff here
Toast.makeText(MainForm.this, "Ще проработя ;) ", Toast.LENGTH_LONG).show();
}
mDrawerLayout.closeDrawers();
// Add code here to update the UI based on the item selected
// For example, swap UI fragments here
return true;
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是针对mToggle元素的
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.open, R.string.close);