Java 使用片段替换布局?

Java 使用片段替换布局?,java,android,Java,Android,这是另一个我很感激的noob问题,我一直在努力完成一项新任务。目前,此页面上有一个名为日语的ImageButton,单击后,我希望它将当前的ScrollView布局替换为显示一些新内容的新布局。毕竟,活动应该保持不变,因为我正在尝试替换ScrollView布局,不是吗?我一直在想一种使用片段来实现这一点的方法,但不知道如何实现 我能想到的唯一一个例子是Spotify的界面,如果你在主页上单击一个图像,它会“替换”该布局以显示一些新歌或其他内容,但不会影响底部导航 到目前为止,我已经看过这些视频,

这是另一个我很感激的noob问题,我一直在努力完成一项新任务。目前,此页面上有一个名为日语的ImageButton,单击后,我希望它将当前的ScrollView布局替换为显示一些新内容的新布局。毕竟,活动应该保持不变,因为我正在尝试替换ScrollView布局,不是吗?我一直在想一种使用片段来实现这一点的方法,但不知道如何实现

我能想到的唯一一个例子是Spotify的界面,如果你在主页上单击一个图像,它会“替换”该布局以显示一些新歌或其他内容,但不会影响底部导航

到目前为止,我已经看过这些视频,但不确定它们是否是正确的解决方案:

这是到目前为止我的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:background="#000"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <TextView
        android:id="@+id/toolbartitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Home"
        android:textColor="@android:color/white"
        android:textSize="32sp"
        android:textStyle="bold" />

</android.support.v7.widget.Toolbar>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/the_bottom_navigation"
    android:layout_width="0dp"
    android:layout_height="75dp"
    android:layout_gravity="bottom"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottomnavigationmenu">

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


<ScrollView
    android:id="@+id/scrollView2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="75dp"
    android:layout_marginTop="0dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/toolbar"
    app:layout_constraintVertical_bias="0.0">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"> 

     [content here]

    </android.support.constraint.ConstraintLayout>
</ScrollView>

[此处内容]

您好,因此我建议使用片段来实现这一点。 创建一个主活动,然后为每个视图创建两个不同的片段。 请看这里:


如果您需要一些代码示例,请告诉我。

您好,因此我建议使用片段来实现这一点。 创建一个主活动,然后为每个视图创建两个不同的片段。 请看这里:


如果您需要一些代码示例,请告诉我。

Hi,那么对于我当前的代码,我也必须将它们放入片段中?是的,一个代码示例将很高兴看到这一切是如何工作的!没问题,我将很快提供一个代码示例。但是基本上是的,当你在Android Studio中创建一个新的片段时,它会生成一个Java类(你把逻辑放在这里)和一个XML文件,这是你片段的布局。嗨,那么对于我当前的代码,我也必须把它们放进片段中?是的,一个代码示例将很高兴看到这一切是如何工作的!没问题,我将很快提供一个代码示例。但基本上是的,当你在AndroidStudio中创建一个新片段时,它会生成一个Java类(你把逻辑放在这里)和一个XML文件,这是片段的布局。