Обрезанное название элемента BottomNavigationView - PullRequest
0 голосов
/ 21 ноября 2018

При использовании библиотеки дизайна '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 голосов
/ 22 ноября 2018

Это из-за неподходящего заполнения для активного дочернего представления (возможно, из-за проблемы), вы можете создать такую ​​функцию:

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.

0 голосов
/ 22 ноября 2018

Используйте этот помощник, он использует отражение для изменения свойств TextView (s) BottomNavigationBar!

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