底部导航视图 - 一个活动和两个片段

时间:2017-12-05 13:02:46

标签: android layout bottomnavigationview

我正在尝试在主要活动和两个bottomNavigation之间建立一个fragments。现在我去了一个教程并创建了第三个fragment

以下是bottomNavigation listener

private BottomNavigationView.OnNavigationItemSelectedListener bottom_navigation_listener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId())
            {
                case R.id.Piano:
                    setTitle("Pitch Piano");
                    PianoFragment fragment = new PianoFragment();
                    android.support.v4.app.FragmentTransaction fragmentTransaction1 = getSupportFragmentManager().beginTransaction();
                    fragmentTransaction1.replace(R.id.frame, fragment, "Piano");
                    fragmentTransaction1.commit();
                    return true;

            case R.id.Rhythm:
                setTitle("Rhythm");
                Rhythm fragment2 = new Rhythm();
                android.support.v4.app.FragmentTransaction fragmentTransaction2 = getSupportFragmentManager().beginTransaction();
                fragmentTransaction2.replace(R.id.frame, fragment2, "Rhythm");
                fragmentTransaction2.commit();
                return true;

            case R.id.Intervals:
                setTitle("Intervals and Chords");
                Intervals_and_chords fragment3 = new Intervals_and_chords();
                android.support.v4.app.FragmentTransaction fragmentTransaction3 = getSupportFragmentManager().beginTransaction();
                fragmentTransaction3.replace(R.id.frame, fragment3, "Intervals");
                fragmentTransaction3.commit();
                return true;


        }
        return false;
    }
};

主要活动布局(缩短):

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.stefanawesome.piano.MainActivity">

<RelativeLayout
    android:layout_width="581dp"
    android:layout_height="503dp"
    android:layout_marginBottom="8dp"
    android:layout_marginTop="8dp"
    android:background="#ffffff"
    android:id="@+id/lin_lay"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.031"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintHorizontal_bias="0.5"
    android:layout_marginStart="8dp"
    android:layout_marginEnd="8dp">

   <include
       android:id="@+id/toolbar"
       layout="@layout/tool_bar"
       />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/upperback"
        android:layout_alignStart="@+id/upperback"
        android:layout_below="@+id/myRectangleView"
        app:itemBackground="@color/bottomNavView"
        app:itemIconTint="@drawable/bottomnavbutton"
        app:itemTextColor="@drawable/bottomnavbutton"
        app:menu="@menu/bottom_nav_items" />

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

节奏片段布局:

<FrameLayout 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.stefanawesome.piano.Rhythm">

<!-- TODO: Update blank fragment layout -->



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

    <include
        android:id="@+id/toolbar"
        layout="@layout/rhythmtoolbar"
        />
</RelativeLayout>

</FrameLayout>

间隔片段布局:

<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="com.stefanawesome.piano.Intervals_and_chords">

    <!-- TODO: Update blank fragment layout -->

</FrameLayout>

当底部导航栏位于节奏部分时的屏幕截图:

enter image description here

主要活动布局不会在主要活动和两个fragments之间跳转,即使您转到fragments fragment布局之一,也不会消失是主要布局的背后。

另外,如果我在任何fragments中添加了代码,程序就会崩溃。

0 个答案:

没有答案