【问题标题】:Android Java Bottom Navigation Bar Fragment not showingAndroid Java底部导航栏片段未显示
【发布时间】:2021-04-16 19:02:45
【问题描述】:

我正在尝试实现底部导航栏,但是即使调用了所有函数,也不会显示片段视图。 这是我的代码,请指出我做错了什么或遗漏了什么。 我当时只测试一个片段。下面给出的代码用于活动、片段和活动布局。

主要活动

public class mActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.m_activity);
    bottomNavigationView = findViewById(R.id.navigationbar);
    bottomNavigationView.setSelectedItemId(R.id.profilenav);
    bottomNavigationView.setOnNavigationItemSelectedListener(listener);
    loadFragment(new HomeFragment());
}

private final BottomNavigationView.OnNavigationItemSelectedListener listener = new BottomNavigationView.OnNavigationItemSelectedListener() {
    @SuppressLint("NonConstantResourceId")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.explorenav:
                //Load explore fragment here
                break;
            case R.id.profilenav:
                loadFragment(new HomeFragment());
                Log.d("HomeFragment","Selected");
                return true;
        }
        return false;
    }
};

private void loadFragment(Fragment fragment) {
    // load fragment
    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    transaction.replace(R.id.m_fragment, fragment);
    transaction.addToBackStack(null);
    transaction.commit();
    Log.d("mActivity","loadfragment()");
}

片段

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.d("HomeFragment","onCreateView Start");
        super.onCreateView(inflater,container,savedInstanceState);
        View v= inflater.inflate(R.layout.home_activity, container, false);
    return v;
}

m_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/includenav"
        layout="@layout/bottom_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="1dp"
        android:layout_gravity="bottom"
        />
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="3dp"
        android:id="@+id/m_fragment"
        />
</androidx.appcompat.widget.LinearLayoutCompat>

bottom_navigation_bar.xml 包含布局的代码:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigationbar"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="bottom"
    android:background="?android:attr/windowBackground"
    android:foreground="?attr/selectableItemBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.994"
    app:menu="@menu/navigation" />
    </androidx.constraintlayout.widget.ConstraintLayout>

这是主代码文件,但是m_fragment框架布局中没有更新布局。

【问题讨论】:

    标签: java android android-fragments fragment bottomnavigationview


    【解决方案1】:

    您正在为 m_activity 使用线性布局并且没有为其指定方向,因此默认为水平,并且您的第一个视图(包含)采用整个宽度(匹配父级), 所以我建议你使用框架布局或约束布局来获得你想要的

    建议的解决方案:

    <?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/m_fragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_margin="3dp"
            app:layout_constraintBottom_toTopOf="@id/includenav"
            app:layout_constraintTop_toTopOf="parent" />
    
        <include
            android:id="@+id/includenav"
            layout="@layout/bottom"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="1dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 用最后的m_activity.xml代码更新问题,请看
    • 感谢帮助这个新手理清概念,完美运行!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多