Android——如何在底部导航视图中设置菜单项?
我有一个底部导航视图,有4个项目 我希望在每个底部导航视图按钮中间设置项目,我找到了一个方法,但是只有当应用程序启动时它才能工作。< /P> 在我选择另一个项目后,每个图标都会向上移动,我不知道为什么它们没有保持它们的位置Android——如何在底部导航视图中设置菜单项?,android,menuitem,android-icons,bottomnavigationview,Android,Menuitem,Android Icons,Bottomnavigationview,我有一个底部导航视图,有4个项目 我希望在每个底部导航视图按钮中间设置项目,我找到了一个方法,但是只有当应用程序启动时它才能工作。< /P> 在我选择另一个项目后,每个图标都会向上移动,我不知道为什么它们没有保持它们的位置 public类MainActivity扩展了AppCompatActivity{ 私有工具栏mToolbar; 私有BottomNavigationView BottomNavigationView; @凌驾 创建时受保护的void(Bundle savedInstanc
public类MainActivity扩展了AppCompatActivity{
私有工具栏mToolbar;
私有BottomNavigationView BottomNavigationView;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar=(工具栏)findviewbyd(R.id.customToolbar);
设置支持操作栏(mToolbar);
片名(“”);
mToolbar.setNavigationIcon(R.mipmap.back_图标);
bottomNavigationView=(bottomNavigationView)findViewById(R.id.bottom\u导航);
BottomNavigationViewShiftDisable.disableShiftMode(bottomNavigationView);
bottomNavigationView.setOnNavigationItemSelectedListener(新的bottomNavigationView.OnNavigationItemSelectedListener(){
@凌驾
公共布尔值onNavigationItemSelected(@NonNull MenuItem item){
片段=空;
开关(item.getItemId()){
案例R.id.profile:
fragment=新的FirstFragment();
打破
案例R.id.friends:
fragment=新的SecondFragment();
打破
案例R.id.circle:
片段=新的第三个片段();
打破
案例R.id设置:
fragment=新的ForthFragment();
打破
}
FragmentTransaction=getSupportFragmentManager().beginTransaction();
事务.替换(R.id.frame_布局,片段);
commit();
setMenuIconsInMiddle(底部导航视图);
返回true;
}
});
bottomNavigationView.setSelectedItemId(R.id.profile);
}
/**
*此方法用于为菜单中在中使用的所有图标设置边距
*底部导航视图。
*@param navigationView底部导航视图的一个实例,该视图使用
*图标。
*/
public void setMenuIconsInMiddle(底部导航视图导航视图){
BottomNavigationMenuView menuView=(BottomNavigationMenuView)navigationView.getChildAt(0);
对于(int index=0;index调用线程上的setmenuiconsisnmidle。请尝试以下操作:-
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//your code here
...
...
...
//Looper.getMainLooper() runs on main thread
new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
setMenuIconsInMiddle(bottomNavigationView);
}
});
return true;
}
});
这对我有用…请添加布局xml以及menu@SachinRao我也添加了布局。
<?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="com.example.hermes.profilescreen.MainActivity">
<include layout="@layout/custom_toolbar"/>
<include layout="@layout/bottom_navigation"/>
</RelativeLayout>
<?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/profile"
android:enabled="true"
android:icon="@drawable/ic_people_outline_black_24dp"
app:showAsAction="ifRoom"
android:title=""
/>
<item
android:id="@+id/friends"
android:enabled="true"
android:icon="@drawable/ic_person_black_24dp"
app:showAsAction="ifRoom"
android:title=""
/>
<item
android:id="@+id/circle"
android:enabled="true"
android:icon="@drawable/ic_panorama_fish_eye_black_24dp"
app:showAsAction="ifRoom"
android:title=""
/>
<item
android:id="@+id/settings"
android:enabled="true"
android:icon="@drawable/setting_white"
app:showAsAction="ifRoom"
android:title=""
/>
</menu>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout"
android:layout_above="@+id/bottom_navigation"
>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="@color/light_black"
app:itemBackground="@drawable/set_backgorund"
app:itemIconTint="@color/item_state"
app:menu="@menu/bottom_nav_main"
/>
</RelativeLayout>
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
//your code here
...
...
...
//Looper.getMainLooper() runs on main thread
new Handler(Looper.getMainLooper()).post(new Runnable(){
@Override
public void run() {
setMenuIconsInMiddle(bottomNavigationView);
}
});
return true;
}
});