猿问

对于BottomNavigationView,如何管理菜单图标和文本之间的差距

我增加了“菜单图标”的大小,这就是它与“菜单文本”重叠的原因。


因此,为此我使用以下方法“menuAlignment()”管理了它们之间的差距,但不知何故在点击“菜单项”后,“菜单图标”和“菜单文本”之间的差距恢复到以前的状态,即重叠,请帮我解决这个问题。


我的代码如下:


    public void menuAlignment()

    {

        for (int i = 0; i < menuView.getChildCount(); i++) {

            final View iconView = menuView.getChildAt(i).findViewById(android.support.design.R.id.icon);


            final FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) iconView.getLayoutParams();

            final DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

            // set your height here

            layoutParams.height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 27, displayMetrics);

            // set your width here

            layoutParams.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 28, displayMetrics);


            layoutParams.gravity=Gravity.CENTER_HORIZONTAL;

            layoutParams.setMargins(0,0,0,200);


            iconView.setLayoutParams(layoutParams);


            //  iconView.setPadding(0, 0, 0, 10);

        }

}

“ navigationClickListener ”的代码:-


private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener

            = new BottomNavigationView.OnNavigationItemSelectedListener() {


        @Override

        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId()) {

                case R.id.navigation_jokes_word:

                    loadFragment(new JokesFragment());

                    menuAlignment();

                    return true;

                case R.id.navigation_jokes_images:

                    loadFragment(new ImageJokesFragment());

                    menuAlignment();

                    return true;

            }

            return false;

        }

    };

蝴蝶不菲
浏览 329回答 2
2回答

牧羊人nacy

菜单项视图<?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&nbsp; &nbsp; android:id="@+id/action_home"&nbsp; &nbsp; android:icon="@drawable/ic_home_blue_48dp"&nbsp; &nbsp; android:title="Home" /><item&nbsp; &nbsp; android:id="@+id/action_menu"&nbsp; &nbsp; android:icon="@drawable/ic_apps_black_24dp"&nbsp; &nbsp; android:title="Menu"&nbsp; &nbsp; &nbsp;/><item&nbsp; &nbsp; android:id="@+id/action_msg"&nbsp; &nbsp; android:icon="@drawable/ic_chat_black_24dp"&nbsp; &nbsp; android:title="Message Inbox"&nbsp; &nbsp;/></menu>单击侦听器bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public boolean onNavigationItemSelected(@NonNull MenuItem item)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Fragment selectedFragment = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; switch (item.getItemId())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_home:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*hide extra toolbars*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideViews();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /*hide back button*/&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setDisplayHomeAsUpEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedFragment = new TimelineFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_menu:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideViews();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setDisplayHomeAsUpEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedFragment = new HomeFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; case R.id.action_msg:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hideViews();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; getSupportActionBar().setDisplayHomeAsUpEnabled(false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selectedFragment = new ChatFragment();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

largeQ

使用在菜单项中设置的自定义布局,例如:<itemandroid:id="@+id/menu_item_1"&nbsp;app:showAsAction="always"app:actionLayout="@layout/custom_menu_item_1" />创建一个布局文件并将此文件设置为 app:actionLayout。custom_menu_item_1.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:src="@drawable/menu_drawable"&nbsp; &nbsp; android:orientation="vertical">&nbsp; &nbsp; <ImageView ... marginBottom=".." />&nbsp; &nbsp; <TextView&nbsp; ... /></LinearLayout>
随时随地看视频慕课网APP

相关分类

Java
我要回答