android - 菜单项图标下的空标题空间

标签 android

我使用了这样的菜单项:

<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"/>

这是上面项目中使用的 drawable 之一,比如 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” 所以底部栏现在有图标并且图标下方没有文本但它仍然有空标题空间导致图标变小 那么我怎样才能设置没有标题,比如让标题空间消失,并且不会占用空间并保留所有空间以使图标比现在更大

这是我底栏的照片:

enter image description here

你可以看到图标下空标题的空间所以如何为图标制作所有空间,比如电 View 标周围的粉红色方 block 以使其更大,因为我试图改变项目和其他解决方案的宽度和高度但没有工作

最佳答案

尝试使用这种方法:

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

关于android - 菜单项图标下的空标题空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51703371/

相关文章:

android向上导航栏总是创建新的 Activity

java - 没有回调接口(interface)的AsyncTask

android - 音频流无法在智能手机中播放

java - 在 Android 中通过 Intent 使用 Zxing 扫描条码时如何更改文本?

android - 如何比较 3 个数组列表并替换第三个数组列表中的数据?

android - 滚动 ListView 时选定的项目问题

android在 ListView 中的按钮上设置onclicklistener

android - Android 应用程序中的 Scala REPL

android - 在 Android 设备的“联系人”>“设置”中获取选定用户的排序偏好以及如何减少查询时间?

android - onActivityResult() 未被调用。我无法显示图片