【问题标题】:Android BottomNavigationView not showsAndroid BottomNavigationView 不显示
【发布时间】:2022-02-03 13:15:34
【问题描述】:

我想在我的 Android 应用程序中实现 BottomNavigationView,但我的代码没有显示此 BottomNavigationView。这是我的代码。

MainActivity.java

public class MainActivity extends AppCompatActivity {


private BottomNavigationView bottomNavView;
private int startingPosition, newPosition;
private final FragmentHome fragmentHome = new FragmentHome();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main_activity);

    bottomNavView = findViewById(R.id.bottom_navigation);
    bottomNavView.setItemIconTintList(null);
    bottomNavView.setOnItemSelectedListener(item -> {
        showFragment(item.getItemId());
        return true;
    });
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.content_frame, fragmentHome);
    bottomNavView.setSelectedItemId(R.id.action_home);
    ft.commit();
}

public void showFragment(int viewId) {
    Fragment fragment = null;

    switch (viewId) {
        case R.id.action_home:
            if (bottomNavView.getSelectedItemId() != R.id.action_home) {
                fragment = fragmentHome;
                newPosition = 0;
            }
            break;
    }

    if (fragment != null) {
        if (startingPosition > newPosition) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.content_frame, fragment).commit();
        }
        if (startingPosition < newPosition) {
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.content_frame, fragment).commit();
        }

        startingPosition = newPosition;
    }
}

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.menu_action_bar, menu);
    final MenuItem searchItem = menu.findItem(R.id.action_search);
    SearchView searchView = (SearchView) searchItem.getActionView();
    searchView.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
        @Override
        public void onViewAttachedToWindow(View arg0) {
            setItemsVisibility(menu, searchItem, false);
        }

        @Override
        public void onViewDetachedFromWindow(View arg0) {
            setItemsVisibility(menu, searchItem, true);
        }
    });
    return super.onCreateOptionsMenu(menu);
}

private void setItemsVisibility(Menu menu, MenuItem exception, boolean visible) {
    for (int i = 0; i < menu.size(); ++i) {
        MenuItem item = menu.getItem(i);
        if (item != exception) item.setVisible(visible);
    }
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    return super.onOptionsItemSelected(item);
}
}

main_activity.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/content_frame"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/darker_gray"
    app:itemIconTint="@android:color/white"
    app:itemTextColor="@android:color/white"
    app:layout_constraintBottom_toBottomOf="parent"
    app:menu="@menu/menu_bottom_navigation" />

menu_bottom_navigation

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/action_home"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/home" />
    <item
        android:id="@+id/action_words_ranking"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/words_ranking" />
    <item
        android:id="@+id/action_users_ranking"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/users_ranking" />
    <item
        android:id="@+id/action_messages"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/messages" />
    <item
        android:id="@+id/action_notifications"
        android:icon="@drawable/ic_launcher_foreground"
        android:title="@string/notifications" />
</menu>

这个BottomNavigationView不仅不显示它甚至不占用任何垂直空间。

请帮帮我!

编辑

我还简化了我的日/夜主题,但没有效果。

编辑 2

我知道在哪里! ft.replace(R.id.content_frame, fragmentHome); 有问题在创建。它显示片段中的内容,但跳过 MainActivity 中的 BottomNavView 但是如何解决呢?

FragmentHome.java

public class FragmentHome extends Fragment {

    private MyViewModel loginViewModel;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_home, container, false);
        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initBinding();

        loginViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String string) {
                Toast.makeText(requireActivity(), "cześć " + string, Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void initBinding() {
        FragmentHomeBinding binding = DataBindingUtil.setContentView((requireActivity()), R.layout.fragment_home);
        loginViewModel = new ViewModelProvider(this).get(MyViewModel.class);
        binding.setMyViewModel(loginViewModel);
        binding.setLifecycleOwner(this);
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="myViewModel"
            type="pl.jawegiel.abc.viewmodel.MyViewModel" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/b"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            android:onClick="@{ ()-> myViewModel.setText() }"/>

        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@={myViewModel.text}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

【问题讨论】:

  • 这会崩溃还是不显示任何内容。因为我重新填充了您的代码,它显示了底部导航。
  • 没有崩溃,只是在 Android Studio 布局预览和 pthone 中都没有显示
  • bottomNavView.setOnItemSelectedListener(item -> { showFragment(item.getItemId()); return true; }); --------------> 将此代码替换为 ::::::::> bottomNavView.setOnNavigationItemSelectedListener(item -> { showFragment(item.getItemId()); return true; }) ;
  • 谢谢,但问题不在于点击项目而是显示onStart
  • 你在重写 onStart() 方法吗?

标签: java android bottomnavigationview


【解决方案1】:

它是否返回任何错误?你的代码没有问题。你能检查一下日志吗?

【讨论】:

  • 它不返回任何东西。没有错误只是不会在手机和 Android Studio 布局预览中显示。
  • 请把这些东西写在 cmets @Nisa
  • 答案部分是给出解决方案。不要问。问请使用 cmets
  • 你说得对,我忘了。谢谢你。 @SambhavKhandelwal
  • 是的,正如我在@NisaEfendioglu 的一篇文章中提到的那样:)现在我在新主题stackoverflow.com/questions/70976615/… 中遇到了另一个问题:D
【解决方案2】:

尝试添加 layout_constraintTop_toBottomOf="@+id/content_frame"

【讨论】:

  • 谢谢,但没有效果:(
【解决方案3】:

app:layout_constraintBottom_toTopOf="@id/bottom_navigation" 行添加到FrameLayout 并将其layout_height 属性更改为0dpFrameLayout必须指定limit,否则会覆盖BottomNavigationBar

【讨论】:

  • 谢谢,但您看到已编辑的 2 个帖子了吗? :)
【解决方案4】:

已解决。问题在于片段中的 DataBinding。我需要移动 initBinding() 并在 onCreateView() 中返回 binding.getRoot()。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多