添加新片段时显示的按钮

黑帽武士

我在另一个片段(片段A->片段B)的顶部添加了一个片段,但是当我这样做时,第一个片段的按钮就会显示出来。我不明白为什么在另一个片段上添加一个片段会显示第一个片段的内容。

这是第一个活动。它包含第一个片段(片段A):

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

以下是片段A的代码,它调用了第二个片段(片段B):

public class MainActivityFragment extends Fragment {

        public MainActivityFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_main, container, false);
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            Button startButton = (Button) view.findViewById(R.id.start_button);
            startButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    GameSetupFragment newGame = new GameSetupFragment();
                    getFragmentManager().beginTransaction()
                            .addToBackStack("MainActivity")
                            .replace(R.id.fragment, newGame)
                            .commit();
                }
            });
        }

    }

这是片段B的类:

public class GameSetupFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

        return inflater.inflate(R.layout.game_board_fragment, container, false);
    }
}

以下是每个文件的xml文件:

Activity_Main.xml(主要活动):

    <android.support.design.widget.CoordinatorLayout 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"
        tools:context="com.blaine.tictactoe.MainActivity">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.AppBarLayout>

        <include layout="@layout/content_main" />

        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fab"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|end"
            android:layout_margin="@dimen/fab_margin"
            app:srcCompat="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>

这是片段B的xml(fragment_main.xml)

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/welcome"
            android:textAppearance="@android:style/TextAppearance.Large"
            android:id="@+id/textView"
            tools:layout_constraintTop_creator="1"
            tools:layout_constraintRight_creator="1"
            android:layout_marginStart="69dp"
            android:layout_marginEnd="69dp"
            app:layout_constraintRight_toRightOf="parent"
            android:layout_marginTop="74dp"
            tools:layout_constraintLeft_creator="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <Button
            android:id="@+id/start_button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/start"
            tools:layout_constraintRight_creator="1"
            tools:layout_constraintBottom_creator="1"
            android:layout_marginStart="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginEnd="8dp"
            app:layout_constraintRight_toRightOf="parent"
            tools:layout_constraintLeft_creator="1"
            android:layout_marginBottom="117dp"
            app:layout_constraintLeft_toLeftOf="parent" />

        <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline"
            android:orientation="vertical"
            app:layout_constraintGuide_begin="367dp"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="367dp" />

        <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline2"
            app:layout_constraintGuide_begin="20dp"
            android:orientation="vertical"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="20dp" />

        <android.support.constraint.Guideline
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/guideline3"
            app:layout_constraintGuide_begin="74dp"
            android:orientation="horizontal"
            tools:layout_editor_absoluteY="74dp"
            tools:layout_editor_absoluteX="0dp" />



</android.support.constraint.ConstraintLayout>

Content_main.xml:

<fragment 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/fragment"
    android:name="com.blaine.tictactoe.fragments.MainActivityFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:layout="@layout/fragment_main" />

片段B代码(game_board_fragment.xml):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@color/colorAccent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is just a test"/>
</RelativeLayout>

应用运行时的外观如下:

第一屏

第二屏

拉胡尔·沙玛(Rahul Sharma)

在您的Content_main.xml中,使用FrameLayout并尝试在其中打开您的片段。例如

<FrameLayout
        android:id="@+id/main_fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

MainActivityonCreate,使用下面的代码打开第一个片段,如下所示:

String fragmentName = targetFragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
Fragment targetFragment = new MainActivityFragment();
manager.popBackStack();
manager.beginTransaction()
                .replace(R.id.main_fragment, targetFragment, fragmentName)
                .addToBackStack(fragmentName)
                .commit();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章