android - 如何在底部导航视图中设置菜单项?

时间:2017-06-12 10:10:24

标签: android menuitem android-icons bottomnavigationview

我有一个包含4个项目的底部导航视图。

我希望在每个底部导航视图按钮的中间设置项目,我找到了一个方法,但它只在应用程序启动时才有效。

在我选择另一个项目后,每个图标都会向上移动,我不知道为什么他们没有保持自己的位置。

This is when the app starts This is when I choose another option

public class MainActivity extends AppCompatActivity{
    private Toolbar mToolbar;
    private BottomNavigationView bottomNavigationView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar = (Toolbar) findViewById(R.id.customToolbar);
        setSupportActionBar(mToolbar);
        setTitle("");
        mToolbar.setNavigationIcon(R.mipmap.back_icon);

        bottomNavigationView = (BottomNavigationView) findViewById(R.id.bottom_navigation);
        BottomNavigationViewShiftDisable.disableShiftMode(bottomNavigationView);

        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Fragment fragment = null;
                switch (item.getItemId()) {
                    case R.id.profile:
                        fragment = new FirstFragment();
                        break;
                    case R.id.friends:
                        fragment = new SecondFragment();
                        break;
                    case R.id.circle:
                        fragment = new ThirdFragment();
                        break;
                    case R.id.settings:
                        fragment = new ForthFragment();
                        break;
                }
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.frame_layout, fragment);
                transaction.commit();
                setMenuIconsInMiddle(bottomNavigationView);
                return true;
            }

        });
        bottomNavigationView.setSelectedItemId(R.id.profile);
    }

    /**
     *  This method is used to set margins for all the icons in the menu that is used in the
     * bottom navigation view.
     * @param navigationView an instance of the Bottom navigation view that holds the menu with 
     *                       the icons.
     */
    public void setMenuIconsInMiddle(BottomNavigationView navigationView){
        BottomNavigationMenuView menuView = (BottomNavigationMenuView)navigationView.getChildAt(0);    

        for(int index = 0; index <menuView.getChildCount(); index++){         
            final View iconView = menuView.getChildAt(index).findViewById(android.support.design.R.id.icon);                
            final ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)iconView.getLayoutParams();    
            layoutParams.setMargins(0,50,0,0);    
            navigationView.requestLayout();
        }
    }

activity_main.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="com.example.hermes.profilescreen.MainActivity">

    <include layout="@layout/custom_toolbar"/>
    <include layout="@layout/bottom_navigation"/>
</RelativeLayout>

bottom_nav_main.xml

<?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>

bottom_navigation.xml

<?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>

1 个答案:

答案 0 :(得分:0)

在线程上调用setMenuIconsInMiddle。试试这个: -

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;
        }
    });

这对我有用......