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



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

该视频将清楚地帮助您了解我的问题是什么 点击观看。

因为我想这是考虑 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>

应用主.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. 从框架布局中删除layout_above。 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>

万一框架布局的行为不正确,然后用相对布局包装它,尽管我认为你不需要这样做。

我已经看过你的视频了。我复制了您的代码并在我的工作区中运行它,但它工作正常。所以我实际上不知道问题出在哪里。您是否考虑使用ConstraintLayout?我尝试将您的xml布局更改为约束布局,它也可以正常工作。

<?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父级底部的约束将使其保持在底部。并且不要忘记将您的FrameLayout与BottomNavigationView进行约束,以便它们不会相互叠加。

我面临着同样的问题。这主要是因为在内部屏幕中使用了协调器布局。任何具有协调器布局的屏幕都会使底部导航(放置在顶部(从其位置移动。

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

在四个片段中,其中一个片段我的意思是最后一个片段将协调器布局作为父布局,以便使底部栏向下推。

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

并且您必须将以下属性添加到协调器布局中。

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

最新更新