Android Java底部导航栏片段未显示
我试图实现一个底部导航栏,但是,即使调用了所有函数,片段视图也没有显示。 这是我的代码,请指出我做错了什么或遗漏了什么。 我一次只测试一个碎片。下面给出的代码用于活动、片段和活动布局
主要活动
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_活动。xml
<?xml version="1.0" encoding="utf-8"?>
<安卓x.appcompat.widget.LinearLayoutCompat
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓" 安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<include
安卓:id="@+id/includenav"
layout="@layout/bottom_navigation_bar"
安卓:layout_width="match_parent"
安卓:layout_height="wrap_content"
安卓:layout_margin="1dp"
安卓:layout_gravity="bottom"
/>
<FrameLayout
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:layout_margin="3dp"
安卓:id="@+id/m_fragment"
/>
</安卓x.appcompat.widget.LinearLayoutCompat>
底部导航栏。xml 包含布局的代码:
<?xml version="1.0" encoding="utf-8"?>
<安卓x.constraintlayout.widget.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
xmlns:app="http://schemas.安卓.com/apk/res-auto">
<com.google.安卓.material.bottomnavigation.BottomNavigationView
安卓:id="@+id/navigationbar"
安卓:layout_width="match_parent"
安卓:layout_height="50dp"
安卓:layout_gravity="bottom"
安卓:background="?安卓:attr/windowBackground"
安卓: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" />
</安卓x.constraintlayout.widget.ConstraintLayout>
这是主代码文件,但是布局不会在m_片段框架布局中更新
# 1 楼答案
您正在为m_活动使用线性布局,但没有指定其方向,因此默认为水平,并且您的第一个视图(包含)采用整个宽度(匹配父视图), 所以我建议你使用框架布局或约束布局来获得你想要的结果
建议的解决方案: