我遇到了一個小問題,因爲我試圖將4個圖標添加到Android的底部導航欄中。Android底部導航欄4個項目在某些設備上顯示不正確
On some devices it looks like this
But I want it to be looking like this
如何實現這一目標?在此先感謝
不知道你需要哪個代碼。對於底部導航欄
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_stdplan:
viewPager.setCurrentItem(0);
break;
case R.id.action_klausur:
viewPager.setCurrentItem(1);
break;
case R.id.action_hausaufgaben:
viewPager.setCurrentItem(2);
break;
case R.id.action_fehlzeiten:
viewPager.setCurrentItem(3);
break;
}
return false;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
if (prevMenuItem != null) {
prevMenuItem.setChecked(false);
}
else
{
bottomNavigationView.getMenu().getItem(0).setChecked(false);
}
Log.d("page", "onPageSelected: "+position);
bottomNavigationView.getMenu().getItem(position).setChecked(true);
prevMenuItem = bottomNavigationView.getMenu().getItem(position);
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
setupViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
stdplanFragment =new StdplanFragment();
klausurenFragment =new KlausurenFragment();
hausaufgabenFragment =new HausaufgabenFragment();
fehlzeitenFragment =new FehlzeitenFragment();
adapter.addFragment(stdplanFragment);
adapter.addFragment(klausurenFragment);
adapter.addFragment(hausaufgabenFragment);
adapter.addFragment(fehlzeitenFragment);
viewPager.setAdapter(adapter);
}
}
bottom_navigation.xml使用的viewpager剛剛添加的一切:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_stdplan"
android:checked="true"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/tab1"
app:showAsAction="never" />
<item
android:id="@+id/action_klausur"
android:checked="false"
android:icon="@android:drawable/ic_menu_agenda"
android:title="@string/tab2"
app:showAsAction="never" />
<item
android:id="@+id/action_hausaufgaben"
android:checked="false"
android:icon="@android:drawable/ic_menu_manage"
android:title="Hausaufgaben"
app:showAsAction="never" />
<item
android:id="@+id/action_fehlzeiten"
android:checked="false"
android:icon="@android:drawable/ic_menu_recent_history"
android:title="@string/tab3"
app:showAsAction="never" />
</menu>
請添加代碼。 – Ahmed
@Ahmed完成添加代碼。不知道要添加什麼 – floreich
任何會查看您的代碼的人都會想要查看添加底部導航的位置和方式。 – Ahmed