通过naviagtion抽屉中的片段替换内容的问题

时间:2017-03-27 05:21:53

标签: android android-fragments android-navigation-drawer

我有一个framelayout,其中有几个子节点,但是当我用片段替换它时,它不会替换它,而是与framelayout xml文件重叠。我已经阅读了一些文档,但我找不到合适的文档。

小帮助会很棒。谢谢!

public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_notification) {
            Fragment fragment = NotificationFragment.newInstance();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.content_frame, fragment).commit();}
        return super.onOptionsItemSelected(item);
    }

xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#bb4297f2"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.bulletin.theinvincible.nautical.MainActivity"
    tools:showIn="@layout/app_bar_main">


    <FrameLayout

        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
            android:orientation="vertical">

            <TextView
                android:id="@+id/hello"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center_horizontal"
                android:text="Hi Ashu, What does your yacht need today?"
                android:textSize="25sp" />

            <Button

                android:background="@drawable/button_round"
                android:id="@+id/button_bookslip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Book a Slip" />

            <Button
                android:id="@+id/button_hireCaptain"
                android:background="@drawable/button_round"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Hire a Captain" />

            <Button
                android:id="@+id/button_hireCrewMember"
                android:background="@drawable/button_round"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="Hire a Crew Member" />

            <Button
                android:id="@+id/button_more"
                android:background="@drawable/button_round"

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:text="More..." />

            <LinearLayout

                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom"
                    android:layout_marginLeft="50dp"
                    android:text="Provide Services to Boat Owners? Get Work Here" />
            </LinearLayout>
        </LinearLayout>

    </FrameLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

请尝试以下代码:

public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_notification) {
            FragmentManager fm = getFragmentManager();
            fm.beginTransaction().replace(R.id.content_frame, new NotificationFragment()).commit();
        return super.onOptionsItemSelected(item);
    }

希望这有帮助。