简体   繁体   English

在片段之间切换时底部导航栏向下推

[英]Bottom Navigation Bar pushes down when changing between fragments

In my activity i have a bottom navigation bar and the frame layout to show the fragments everything works fine but the problem is when i start moving from 1 - 4 in sequence the bottom navigation bar stays in its position but when i jump suddenly from 4 to 2 then the bottom navigation bar goes out of screen and when again clicked on the same item then it comes to normal position.在我的活动中,我有一个底部导航栏和框架布局来显示片段一切正常,但问题是当我开始按顺序从 1 - 4 移动时,底部导航栏保持在其位置,但是当我突然从 4 跳到2 然后底部导航栏退出屏幕,再次单击同一项目时,它会回到正常位置。

This video will clearly help you get what my problem is Click to watch.该视频将清楚地帮助您了解我的问题是什么点击观看。

as i guess this is a major problem when considering the UI so kindly help me how can i achieve this.因为我想这是考虑 UI 时的一个主要问题,所以请帮助我如何实现这一目标。 For making things easier i'm posting my codes which contain these elements.为了让事情更容易,我发布了包含这些元素的代码。

activity_appMain.xml activity_appMain.xml

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

    <FrameLayout
        android:id="@+id/fragments_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/navigation_bar"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="?android:attr/windowBackground"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_navigation" />

</RelativeLayout>

AppMain.java应用程序主程序

package com.coderedinnovations.allioservices.AppFragments;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MenuItem;
import android.view.WindowManager;

import com.coderedinnovations.allioservices.AppFragments.FeedbackFragment;
import com.coderedinnovations.allioservices.AppFragments.HomeFragment;
import com.coderedinnovations.allioservices.AppFragments.MyOrdersFragment;
import com.coderedinnovations.allioservices.AppFragments.MyProfileFragment;
import com.coderedinnovations.allioservices.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.iid.FirebaseInstanceId;

public class AppMain extends AppCompatActivity {

    public void adjustFontScale(Configuration configuration){

        configuration.fontScale = (float) 0.9;
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        wm.getDefaultDisplay().getMetrics(metrics);
        metrics.scaledDensity = configuration.fontScale * metrics.density;
        getBaseContext().getResources().updateConfiguration(configuration, metrics);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_app_main);
        adjustFontScale(getResources().getConfiguration());
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        BottomNavigationView bottomNavigationView = findViewById(R.id.navigation_bar);
        bottomNavigationView.setOnNavigationItemSelectedListener(navigationItemSelectedListener);

        getSupportFragmentManager().beginTransaction().replace(R.id.fragments_container, new HomeFragment()).commit();
    }

    private BottomNavigationView.OnNavigationItemSelectedListener navigationItemSelectedListener =
            new BottomNavigationView.OnNavigationItemSelectedListener() {
                @Override
                public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                    Fragment selectedFragment = null;

                    switch (menuItem.getItemId()){
                        case R.id.nav_home:
                            selectedFragment = new HomeFragment();
                            break;
                        case R.id.nav_orders:
                            selectedFragment = new MyOrdersFragment();
                            break;
                        case R.id.nav_feedback:
                            selectedFragment = new FeedbackFragment();
                            break;
                        case R.id.nav_profile:
                            selectedFragment = new MyProfileFragment();
                            break;
                    }

                    getSupportFragmentManager().beginTransaction().replace(R.id.fragments_container,selectedFragment).commit();
                    return true;
                }
            };
}

I looked for a similar question like this but none of them have been answered我找了一个类似的问题,但没有一个得到回答

Edit: Issue only appears when i press from back to front but when i'm going from 1-4 the issue doesn't arise but when i click suddenly from 4 to any other tab the bar gets pushed down.编辑:只有当我从后向前按时才会出现问题,但是当我从 1-4 开始时,问题不会出现,但是当我从 4 突然点击到任何其他选项卡时,栏会被推下。

I really thank everyone's effort for helping me in solving the issue.我真的很感谢大家帮助我解决问题的努力。 but somehow i sorted out the problem by my own.但不知何故,我自己解决了这个问题。 In the four fragments one of the fragment I mean the last fragment have the Co-ordinator layout as the parent layout so that what made the bottom bar to push down.在四个片段中,一个片段我的意思是最后一个片段将Co-ordinator 布局作为父布局,以便使底部栏向下推。

so i solved the issue by taking Constraint layout as the parent layout and added the child into that.所以我通过将约束布局作为父布局并将子项添加到其中来解决了这个问题。 thanks for everyone again.再次感谢大家。

Try this:尝试这个:

1.Add bottom margin 55 dp to framelayout. 1.向framelayout添加底部边距55 dp。 2. remove the layout_above from framelayout. 2.从framelayout中删除layout_above。 3. add parent top true, center in parent true. 3.添加父级为真,父级居中为真。

I actually did a layout quite different from yours in a similar enough scenario.实际上,在足够相似的情况下,我做了一个与你完全不同的布局。 Let me modify it and see if this works.让我修改它,看看它是否有效。 In my case instead of a FrameLayout I use a relative layout with a scrollview inside, to allow content of any height.在我的例子中,我使用了一个带有滚动视图的相对布局而不是 FrameLayout,以允许任何高度的内容。 This sets the bottom bar to float at the bottom always.这将底部栏设置为始终浮动在底部。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical">

<FrameLayout
    android:id="@+id/fragments_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigation_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?android:attr/windowBackground"
    app:labelVisibilityMode="labeled"
    app:menu="@menu/bottom_navigation" />

</LinearLayout>

In the off chance the frame layout doesn't behave correctly, then wrap it with a relative layout, though I do not think you shall need to.万一框架布局不正确,然后用相对布局包装它,尽管我认为你不需要。

I'v already watched your video.我已经看过你的视频了。 I copied your code and run it in my workspace, but it works fine.我复制了您的代码并在我的工作区中运行它,但它工作正常。 So I actually don't know where the problem lies.所以我实际上不知道问题出在哪里。 Have you consider using ConstraintLayout ?你有没有考虑使用ConstraintLayout I tried to change your xml layout into ConstraintLayout and it also works fine.我试图将您的 xml 布局更改为 ConstraintLayout 并且它也可以正常工作。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <FrameLayout
        android:id="@+id/fragments_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@id/navigation_bar"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/navigation_bar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:labelVisibilityMode="labeled"
        app:menu="@menu/bottom_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

The constraint of BottomNavigationView to the bottom of parent will make it stay at the bottom. BottomNavigationViewparent 底部的约束将使其停留在底部。 And don't forget to constaint your FrameLayout with BottomNavigationView so that they are not overlayed with each other.并且不要忘记使用BottomNavigationView 约束您的FrameLayout,以便它们不会相互重叠。

I was facing the same problem.我面临同样的问题。 This was mainly because of using coordinatorlayout in the inner screens.这主要是因为在内部屏幕中使用了 coordinatorlayout。 Any screen having coordinatorlayout makes the bottom navigation (placed on top) move from its location.任何具有 coordinatorlayout 的屏幕都会使底部导航(放置在顶部)从其位​​置移动。

https://stackoverflow.com/a/59844009/13124119 - this answer worked for me also! https://stackoverflow.com/a/59844009/13124119 - 这个答案也对我有用!

In the four fragments one of the fragment I mean the last fragment have the Co-ordinator layout as the parent layout so that what made the bottom bar to push down.在四个片段中,一个片段我的意思是最后一个片段将 Co-ordinator 布局作为父布局,以便使底部栏向下推。

so I solved the issue by taking Constraint layout as the parent layout and added the child into that.所以我通过将约束布局作为父布局并将子布局添加到其中来解决了这个问题。

and you have to add the following attributes to Co-ordinator layout.并且您必须将以下属性添加到 Co-ordinator 布局。

app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM