项目BottomNavigationView的标题裁剪

时间:2018-11-21 20:47:35

标签: android navigationbar bottomnavigationview android-bottomnav

使用设计库'com.android.support:design:28.0.0'时BottomNavigationView错误地显示带有长文本的项目标题:

enter image description here 我的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".mvp.ui.activities.MainActivity">

    <android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="56dp"
        android:layout_above="@+id/navigation"
        android:id="@+id/fragmentContent" />

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        app:labelVisibilityMode="labeled"
        app:itemHorizontalTranslationEnabled="false"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

</android.support.constraint.ConstraintLayout>

这看起来很奇怪,因为使用设计库com.android.support:design:27.1.1时使用完全相同的代码会产生很好的结果:

enter image description here

这怎么可能?对于新版本的库,我尝试使用labelVisibilityMode,但这无济于事。

app:showAsAction="always"也不起作用。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

这是因为活动子视图的填充不适当,(可能是一个问题),您可以创建如下函数:

public void removePaddingFromNavigationItem() {
    BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottomNavigationView.getChildAt(0);

    for (int i = 0; i < menuView.getChildCount(); i++) {
        BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);
        View activeLabel = item.findViewById(R.id.largeLabel);
        if (activeLabel instanceof TextView) {
            activeLabel.setPadding(0, 0, 0, 0);
        }
    }
}

并在removePaddingFromNavigationItem之前调用setOnNavigationItemSelectedListener方法。

答案 1 :(得分:0)

使用此帮助器,它使用反射来更改BottomNavigationBar的TextView属性!

public class BottomNavigationViewHelper {
    public static void refineBottomBar(Context context, BottomNavigationView view) {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) view.getChildAt(0);
        try {
            for (int i = 0; i < menuView.getChildCount(); i++) {
                BottomNavigationItemView item = (BottomNavigationItemView) menuView.getChildAt(i);

                Field LargeText = item.getClass().getDeclaredField("mLargeLabel");
                LargeText.setAccessible(true);
                Field SmallText = item.getClass().getDeclaredField("mSmallLabel");
                SmallText.setAccessible(true);
                TextView SmallTextView =(TextView) SmallText.get(item);
                TextView LargeTextView =(TextView) LargeText.get(item);
                SmallTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                LargeTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
                SmallTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                LargeTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
                LargeTextView.setPadding(0, 0, 0, 0);
                SmallTextView.setPadding(0, 0, 0, 0);

            }
        } catch (NoSuchFieldException e) {
            Log.e("BNVHelper", "Unable to get shift mode field", e);
        } catch (IllegalAccessException e) {
            Log.e("BNVHelper", "Unable to change value of shift mode", e);
        }
    }
}