[英]Android Java Bottom Navigation Bar Fragment not showing
我正在嘗試實現一個底部導航欄,但是即使調用了所有函數,也沒有顯示片段視圖。 這是我的代碼,請指出我做錯了什么或遺漏了什么。 我當時只測試一個片段。 下面給出的代碼用於活動、片段和活動布局。
主要活動
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 框架布局中的布局沒有更新。
您正在為 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.