Android Studio 1.4导航抽屉
问题描述:
我是Android应用开发新手。今天,我试图将我的应用程序更新为android新材质设计。所以我使用Android studio(1.4)导航Drawer Activity。问题是我不知道如何使用导航栏来导航我的活动。它与我见过的在线教程不同。它不使用碎片。 我可以更改名称,图标..等问题是我不能理解如何使用导航抽屉活动之间导航?Android Studio 1.4导航抽屉
谢谢
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camara) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
答
我有同样的问题,但得到修复。
按照下面的步骤:
1.open位于 “布局” 文件夹中的 “content_main.xml” 文件。
2.使用代码如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainFrame">
</FrameLayout>
</RelativeLayout>
- 转到 “onNavigationItemSelected” 的方法:
public boolean onNavigationItemSelected(MenuItem item) { int id = item.getItemId(); Fragment fragment = new YourFragment(); if (id == R.id.nav_camara) { FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); ft.replace(R.id.mainFrame, fragment); ft.commit(); } else if (id == R.id.nav_gallery) { } else if (id == R.id.nav_slideshow) { } else if (id == R.id.nav_manage) { } else if (id == R.id.nav_share) { } else if (id == R.id.nav_send) { } //Close Drawer After Action DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.closeDrawer(GravityCompat.START); return true;
答
在这种情况下,这是最好使用片段,每个导航项目,在您的主要活动替换当前的片段,这将是简单的事。请参阅Android开发指南中的片段。
答
你可以更好地使用片段不活动来添加每个导航选项上的内容(在使用Android Studio中的导航抽屉活动选项创建应用程序之后) 。
content_main.xml(分配ID此相对布局,实施例:机器人:ID = “@ + ID/relative_layout_for_fragment”)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.root.netutility.MainActivity"
tools:showIn="@layout/app_bar_main"
android:id="@+id/relative_layout_for_fragment">
<!--<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />-->
</RelativeLayout>
MainActivity.Java(你改变片段这样)
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment = null;
if (id == R.id.ping_tab) {
fragment = new FirstFragment();
toolbar.setTitle("First");
} else if (id == R.id.traceroute_tab) {
fragment = new SecondFragment();
toolbar.setTitle("Second"); // if you wan to change title
}else if (id == R.id.nav_share) {
fragment = new ThirdFragment();
} else if (id == R.id.nav_send) {
fragment = new FourthFragment();
}
if(fragment != null) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.relative_layout_for_fragment, fragment).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
谢谢你们 –
其实,他问了导航抽屉里的活动,但你的代码是用碎片。 – oneNiceFriend