在我的活动中,我有一个底部导航栏和用于显示片段的框架布局,一切正常,但是问题是当我按顺序从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.将下边距55 dp添加到框架布局。
2.从framelayout中删除layout_above。
3.添加父项顶部为true,居中于父项true。

10-08 15:38