无法从导航抽屉项

时间:2016-05-31 22:59:13

标签: android android-fragments android-fragmentactivity

我已经找到了很多这方面但我真的无法解决我的问题。我花了好几个小时但没有。

我使用Android Studio 2.1.1模板制作了导航抽屉活动。当然,当我点击菜单中的某个项目,显示不同的碎片时,我想更改我的应用程序的视图。这是我在MainActivity中的代码:

        public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {

            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();

            new gaussFragment();

        } 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;
    }

当我点击导航抽屉的第一项(标识为R.id.nav_camera的那一项)时,我可以看到吐司,但新的片段没有出现。我在gaussFragment()中使用此代码:

public class gaussFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_gauss, container, false);
    }

}

当然,正如您可以看到here,fragment_gauss.xml是具有以下代码的相对布局:

<FrameLayout 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"
    tools:context="info.androidhive.tabsswipexx.gaussFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />

</FrameLayout>

我怎么解决这个问题?

content_main是我在应用启动时看到的第一件事。我应该尝试添加类似

的内容
<FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

并以某种方式加载我的碎片?

0 个答案:

没有答案
相关问题