繁体   English   中英

Android 底部导航视图未显示

[英]Android BottomNavigationView not shows

我想在我的 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" />

</androidx.constraintlayout.widget.ConstraintLayout>

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) 有问题。 在 onCreate? 它显示片段中的内容,但跳过 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>

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

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

app:layout_constraintBottom_toTopOf="@id/bottom_navigation"行添加到 FrameLayout 并将其layout_height属性更改为0dp 您必须为FrameLayout指定限制,否则它可以覆盖BottomNavigationBar

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

暂无
暂无

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

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