【问题标题】:Bottom Navigation Bar pushes down when changing between fragments在片段之间切换时底部导航栏向下推
【发布时间】:2020-01-10 17:48:19
【问题描述】:

在我的活动中,我有一个底部导航栏和显示片段的框架布局一切正常,但问题是当我开始按顺序从 1 到 4 移动时,底部导航栏保持在其位置但当我突然跳跃时从 4 到 2 然后底部导航栏离开屏幕,当再次单击同一项目时,它会回到正常位置。

此视频将清楚地帮助您解决我的问题Click to watch.

我想这是考虑 UI 时的一个主要问题,所以请帮助我如何实现这一点。为了让事情变得更容易,我发布了包含这些元素的代码。

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;
                }
            };
}

我找了一个类似的问题,但没有一个得到回答

编辑:问题仅在我从后向前按时出现,但当我从 1-4 开始时,问题不会出现,但当我突然从 4 点按到任何其他选项卡时,栏会被向下推。

【问题讨论】:

  • 我的导航视图有一个奇怪的行为,这是我在任何应用程序中都没有见过的。当我从 1 到 4 开始交易时,选项卡会平稳变化,但是当我突然从 4 按下到另一个选项卡时,底部栏会被向下推,当我按下同一个选项卡时,它会再次出现。只有当我从 4 回到任何其他选项卡时才会发生这种情况。我尝试了很多东西,但没有一个能解决问题。请帮助我知道问题出在哪里。

标签: java android android-studio bottomnavigationview


【解决方案1】:

我真的很感谢大家帮助我解决问题的努力。但不知何故,我自己解决了这个问题。在四个片段中,我的意思是最后一个片段将 Co-ordinator 布局 作为父布局,以便将底部栏向下推。

所以我通过将 约束布局 作为父布局并将子布局添加到其中解决了这个问题。再次感谢大家。

【讨论】:

    【解决方案2】:

    试试这个:

    1.在框架布局中添加下边距 55 dp。 2. 从框架布局中删除 layout_above。 3.添加parent top true,center in parent true。

    【讨论】:

    • 我想问题出在 java 代码上,当我开始从一个移动到另一个时,底部栏保持在其位置,但从 4 到任何其他选项卡单击后,栏会向下移动。跨度>
    【解决方案3】:

    实际上,在一个足够相似的场景中,我做了一个与你完全不同的布局。让我修改它,看看这是否有效。在我的情况下,我使用相对布局而不是 FrameLayout,其中包含滚动视图,以允许任何高度的内容。这将底部栏设置为始终浮动在底部。

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

    如果框架布局不正确,则使用相对布局将其包装起来,尽管我认为您不需要这样做。

    【讨论】:

    • 就像在答案中我使用了线性布局但问题仍然存在并且框架布局内的内容没有正确显示
    • 您是否也注意到我的代码中的 layout_width/height 变化?也许尝试用相对布局包装 FrameLayout?
    • 但我想问题出在 java..!!请您检查一次。
    【解决方案4】:

    我已经看过你的视频了。我复制了您的代码并在我的工作区中运行它,但它工作正常。所以我其实不知道问题出在哪里。您是否考虑过使用ConstraintLayout?我尝试将您的 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>
    

    BottomNavigationView父级底部 的约束将使其停留在底部。并且不要忘记使用 BottomNavigationView 约束您的 FrameLayout,以免它们相互重叠。

    【讨论】:

    • 这个问题在评论中已经解释得很清楚了,你甚至可以观看它附带的视频。但是我如何设法使用系统提供的底部导航活动。
    • 我知道,我也已经看过你的视频了。我复制你的代码。并在我的工作区中运行它。但是我没有遇到和你一样的问题。这就是为什么我不明白问题出在哪里。如果您已经找到解决方案,请在您的回答中提供。
    • 我在您提供的代码中尝试了很多东西,但都没有奏效,所以我使用了默认的 Android 底部导航活动,现在我没有找到向下推的栏。不过感谢支持
    【解决方案5】:

    我也遇到了同样的问题。这主要是因为在内部屏幕中使用了 coordinatorlayout。任何具有 coordinatorlayout 的屏幕都会使底部导航(位于顶部)从其位​​置移动。

    【讨论】:

      【解决方案6】:

      https://stackoverflow.com/a/59844009/13124119 - 这个答案也对我有用!

      在四个片段中,其中一个片段的意思是最后一个片段具有 Co-ordinator 布局作为父布局,因此是什么使底部栏向下推。

      所以我通过将约束布局作为父布局并将子布局添加到其中来解决问题。

      您必须将以下属性添加到 Co-ordinator 布局中。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-03-20
        • 1970-01-01
        • 2023-01-20
        • 2019-03-05
        • 2021-11-07
        • 2022-11-04
        • 1970-01-01
        相关资源
        最近更新 更多