繁体   English   中英

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

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

在我的活动中,我有一个底部导航栏和框架布局来显示片段一切正常,但问题是当我开始按顺序从 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>

应用程序主程序

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 突然点击到任何其他选项卡时,栏会被推下。

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

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

尝试这个:

1.向framelayout添加底部边距55 dp。 2.从framelayout中删除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 布局更改为 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>

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

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

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"

暂无
暂无

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

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