我(初学者)刚刚在Android Studio(API19)的这个论坛的帮助下编写了一个底部导航栏,它在模拟器中编译启动,但我无法弄清楚为什么它不会在你点击时切换片段栏中的项目。对于Java中方法中的每个Case,我创建了一个没有任何特殊代码的片段。尽管我在开始时在gradle中实现了设计并添加了一个浮动按钮,但这几乎就是我所做的。
非常感谢您的帮助!
这是我的Java代码
public class MainActivity extends AppCompatActivity {
//Bottom Navigation Bar
Fragment currentFragment = null;
FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bottom Navigation Bar
ft = getSupportFragmentManager().beginTransaction();
currentFragment = new HomeFragment();
ft.replace(R.id.main_frame_layout, currentFragment);
ft.commit();
//Listener Bottom Navigation Bar
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation_bar);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
// Method for Nav. Bar Listener
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener = new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
currentFragment = new HomeFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_frame_layout, currentFragment);
ft.commit();
return true;
case R.id.nav_overview:
currentFragment = new OverviewFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_frame_layout, currentFragment);
ft.commit();
return true;
case R.id.nav_charts:
currentFragment = new ChartsFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_frame_layout, currentFragment);
ft.commit();
return true;
case R.id.nav_targets:
currentFragment = new TargetsFragment();
ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.main_frame_layout, currentFragment);
ft.commit();
return true;
}
return true;
}
};
}
这是我的XML代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/main_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:layout_marginEnd="16dp"
android:layout_marginBottom="72dp"
android:src="@drawable/ic_add_white"
android:layout_gravity="bottom|end"
android:focusable="true"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation_bar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/colorAccent"
app:itemTextColor="@color/colorAccent"
app:menu="@menu/navigation_items"/>
</RelativeLayout>
这是我生成导航项的菜单文件,但我认为它不重要
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/nav_home"
android:icon="@drawable/ic_home_white_24dp"
android:title="@string/nav_home"
android:enabled="true"/>
<item
android:id="@+id/nav_overview"
android:icon="@drawable/ic_format_list_bulleted_white_24dp"
android:title="@string/nav_overview"
android:enabled="true"/>
<item
android:id="@+id/nav_charts"
android:icon="@drawable/ic_insert_chart_white_24dp"
android:title="@string/nav_charts"
android:enabled="true"/>
<item
android:id="@+id/nav_targets"
android:icon="@drawable/ic_monetization_on_white_24dp"
android:title="@string/nav_targets"
android:enabled="true"/>
</menu>