android - 单击抽屉导航项目上的更改屏幕

标签 android android-activity navigation navigation-drawer drawer

我是纯 Android 的新手,来自 Xamarin.Forms,所以这里是非常基本的问题。我使用抽屉导航模板开始了我的项目,并且我试图更改在我选择其中一个抽屉导航项目时默认看到的“hello world”屏幕。我看到 content_main.xml 中有一个定义“Hello World”屏幕的相对布局。如何将我看到的屏幕切换到另一个 xml 布局?像这样

    public boolean onNavigationItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.screen1) {
        // set current screen to content_main.xml
    } else if (id == R.id.screen2) {
        // set current screen to my_new_screen.xml
    } else if (id == R.id.screen3) {
        // set current screen to my_other_new_screen.xml
    }

最佳答案

您可以像这样在项目点击上启动新的 Activity

Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
startActivity(intent);

或者如果您正在使用 fragment ,您可以通过此代码更改 fragment

Fragment newFragment = new YourNewFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.fragment_container_name, newFragment);
transaction.commit();

关于android - 单击抽屉导航项目上的更改屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35619887/

相关文章:

java - 如何定期更改 Canvas 对象的颜色?

android - 如何将数据从服务发送到 Activity ?

android - 哪种方式更适合将大量数据从一个 Activity 传递到另一个 Activity - android?

silverlight - Silverlight 导航的 ViewModel 优先方法

android - 在 Android 上托管网络服务

android - Firestore 将 HashMap 添加到 Firestore 中的映射字段

java - 如何防止android应用程序因后台线程异常而崩溃?

java - Java 中解析 JSON 的最新方法是什么?

wordpress - 如何使导航栏真正全宽和静态

iphone - 使用导航栏时如何将值从 view2 返回到 view1