2017-07-17 72 views
1

所有這些都比較新,這是另一個我會感激的noob問題,我一直在努力完成一項新任務。目前,在這個頁面上有一個叫做Japanese的ImageButton,點擊後我想用一個新的佈局替換當前的ScrollView佈局,顯示一些新的內容。活動應該保持不變,因爲我試圖替換ScrollView佈局,不是嗎?我一直在想一種方法來使用片段來實現這一點,但不知道如何去做。使用片段替換佈局?

我能想到的唯一例子就是Spotify的界面,如果您在主頁上點擊圖片,它會'替換'該佈局以顯示一些新歌或其他內容,但不會影響底部導航。

我觀看這些視頻,到目前爲止,但我不知道他們是正確的解決方案:https://www.youtube.com/watch?v=FF-e6CnBwYY

This is what I'm trying to achieve.

這是到目前爲止我的代碼。

<?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> 

回答

0

嗨所以我建議使用片段來實現這一點。 爲每個視圖創建一個主要活動,然後創建2個不同的片段。 看看這裏:https://developer.android.com/guide/components/fragments.html

如果你想要一些代碼示例讓我知道。

+0

嗨,所以對於我目前的代碼,我將不得不把它們放入一個片段?是的代碼示例將很高興看到這一切如何工作! – MechaKondor

+0

沒問題,我很快會提供一個代碼示例。但基本上是的,當你在Android Studio中創建一個新的片段時,它會生成一個Java類(放置邏輯的地方)和一個XML文件,它是你片段的佈局。 – Aci89