【问题标题】:Space of null title under icons of menu item菜单项图标下的空标题空间
【发布时间】:2018-08-06 08:22:14
【问题描述】:

我是这样使用菜单项的:

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/tab_bookmarks"
    android:icon="@drawable/bookmark"
    android:title="@null"/>
<item
    android:id="@+id/tab_shasha"
    android:icon="@drawable/shasha"
    android:height="54dp"
    android:width="24dp"
    android:title="@null"/>
<item
    android:id="@+id/tab_home"
    android:icon="@drawable/home"
    android:title="@null"/>
<item
    android:id="@+id/tab_tv"
    android:icon="@drawable/tv"
    android:title="@null"/>
<item
    android:id="@+id/tab_more"
    android:icon="@drawable/more"
    android:title="@null"/>

这是上面项目中使用的可绘制对象之一,例如 android:icon="@drawable/tv" 所以这是 tv.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_checked="true"
        android:drawable="@drawable/ic_tab_tv_selected" />
    <item
        android:state_checked="false"
        android:bottom="13dp"
        android:drawable="@drawable/ic_tab_tv"/>
</selector>

我在bottomNavigationView中使用这个菜单是这样的:

BottomNavigationView bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation);

        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

        bottomNavigationView.setOnNavigationItemSelectedListener
                (new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        Fragment selectedFragment = null;
                        switch (item.getItemId()) {
                            case R.id.tab_bookmarks:
                                selectedFragment = BookmarksFragment.newInstance();
                                break;

                            case R.id.tab_home:
                                selectedFragment = AboutUs.newInstance();
                                break;

                        }
                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                        transaction.replace(R.id.frame_layout, selectedFragment);
                        transaction.commit();
                        return true;
                    }
                });

一切正常,但您可以看到菜单项有图标,但我将标题设置为“@null” 所以底部栏现在有图标,图标下方没有文字,但它仍然有空标题的空间,导致图标变小 那么如何设置没有标题,例如让标题空间消失并且不会占用空间并将其全部留给图标比现在更大

这是我底栏的照片:

您可以在图标下看到空标题上的空间,因此如何为图标腾出所有空间,例如电视图标周围的粉红色方块以使其更大,因为我试图更改项目和其他解决方案的宽度和高度,但没有工作

【问题讨论】:

    标签: android


    【解决方案1】:

    试试这个方法:

    public void updateBottomBar() {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);
        if (menuView != null) {
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView menuItemView = (BottomNavigationItemView) menuView.getChildAt(i);
                ImageView icon = menuItemView.findViewById(R.id.icon);
                FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) icon.getLayoutParams();
                params.gravity = Gravity.CENTER;
            }
        }
    }
    

    标签位置改变时必须调用。

    更新

    有一个更漂亮的解决方案,它与支持库 28.0.0-alpha1 一起工作:

     bottomNavigationView.setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_UNLABELED);
    

    <android.support.design.widget.BottomNavigationView
        app:labelVisibilityMode="unlabeled" />
    

    并删除您的代码BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);

    【讨论】:

    • 请您解释一下
    • 上面的代码是在初始化bottomNavigationView之后应用的。他找到图标并将其居中。
    • 所以你的意思是我应该保留我写的代码,只是将你谈到的方法也添加到代码中? @UgAr0FF
    • 谢谢,我尝试了代码方式,但它不起作用,所以我将库更新到 28,它适用于惊人的新解决方案,现在标题隐藏并且图标居中很好,但图标仍然很小在尺寸上,有什么办法可以让它更大吗?如果你能帮助我,非常感谢@UgAr0FF
    • 将这个参数&lt;dimen name="design_bottom_navigation_icon_size" tools:override="true"&gt;24dp&lt;/dimen&gt;添加到dimens.xml中需要的大小,它将覆盖图标大小。
    猜你喜欢
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多