Почему панель инструментов игнорируется после переключения фрагмента в Android? - PullRequest
0 голосов
/ 22 июня 2019

Я создаю приложение с BottomNavigationView, в котором первая вкладка загружена правильно:

Вкладка 1:

img1

Однако, когда я переключаюсь на следующую вкладку, кажется, что Framelayout проходит под панелью инструментов следующим образом:

Вкладка 2:

img 2

Снова вкладка 1:

img 3

Это мой макет:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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">
    <FrameLayout
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" 
        android:layout_above="@+id/bottom_navigation"/>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:menu="@menu/bottom_nav_menu"
        app:labelVisibilityMode="unlabeled"
        app:itemBackground="@color/colorPrimary"
        app:itemIconTint="@color/bottom_nav_color"
        app:itemTextColor="@color/bottom_nav_color" />
</RelativeLayout>

Есть похожие случаи, подобные этому:

Часть элементов фрагмента скрывается под панелью действий

Но в моем случае решения не работают. Любая идея, что я должен изменить? Или почему панель инструментов теряет приоритет? Я уже тестировал, чтобы создать его самостоятельно в Layout, и он тоже не работал должным образом.

приписка

Я загружаю WebViews , но я сомневаюсь, что это влияет.

Ответы [ 3 ]

0 голосов
/ 22 июня 2019

Ваш код работает нормально для меня. Хотя вы можете установить верхнюю границу для макета кадра

android:layout_marginTop="?attr/actionBarSize"

0 голосов
/ 22 июня 2019

Я нашел, как это исправить, используя CoordinatorLayout :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"            
            android:layout_above="@+id/bottom_navigation">
            <FrameLayout
                android:id="@+id/content_frame"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </android.support.design.widget.CoordinatorLayout>
        <android.support.design.widget.BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:menu="@menu/bottom_nav_menu"
            app:labelVisibilityMode="unlabeled"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="@color/bottom_nav_color"
            app:itemTextColor="@color/bottom_nav_color" />
    </RelativeLayout>
</android.support.v4.widget.DrawerLayout>

У меня возникла идея:

https://forums.xamarin.com/discussion/146285/how-to-do-appbarlayout-with-collapsing-toolbarlayout-that-will-collapse-when-viewpager-scroll

0 голосов
/ 22 июня 2019

Я использую его в своем проекте и работаю:

<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=".app.main.MainActivity">

<FrameLayout
    android:id="@+id/frame_fragment_containers"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
    android:layout_above="@id/bottom_navigation"/>




    <android.support.design.widget.BottomNavigationView
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:itemBackground="@color/colorWhite"
        app:itemIconTint="@color/selector_bottom_navigation"
        app:itemTextColor="@color/selector_bottom_navigation"
        android:layout_alignParentBottom="true"
        >
    </android.support.design.widget.BottomNavigationView>

, и вы можете использовать его программно:

 bottomNavigation = (BottomNavigationView) findViewById(R.id.bottom_navigation);
    bottomNavigation.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
    bottomNavigation.inflateMenu(R.menu.bottom_menu);
    // change to whichever id should be default
    if (savedInstanceState == null) {
        bottomNavigation.setSelectedItemId(R.id.home);
    }
    fragmentManager = getSupportFragmentManager();
    bottomNavigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            int id = item.getItemId();
            switch (id) {

                case R.id.profile:
                    fragment = new FragmentD();
                    break;
                case R.id.support:
                    fragment = new FragmentC();
                    break;
                case R.id.order:
                    fragment = new FragmentB();
                    break;
                case R.id.home:
                    fragment = new FragmentA();
                    break;

            }
            final FragmentTransaction transaction = fragmentManager.beginTransaction();
            transaction.replace(R.id.frame_fragment_containers, fragment).commit();
            return true;
        }
    });

и создать новый xml (bottomNavigation.inflateMenu(R.menu.bottom_menu)) в папке меню для пункта BottomNavigation:

<menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">



<item
    android:id="@+id/profile"
    android:title="Profile"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_profile">
</item>
<item
    android:id="@+id/support"
    android:title="Support"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_support">
</item>
<item
    android:id="@+id/order"
    android:title="Order"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_orders">
</item>
<item
    android:id="@+id/home"
    android:title="Home"
    app:showAsAction="ifRoom"
    android:enabled="true"
    android:icon="@drawable/ic_home">
</item>

...