底部导航栏:文字大小在按下时增加?

Bottom Navigation Bar: Text size is increasing on pressing?

我在 Android 中使用底部导航栏。默认情况下,当我 select 一个项目时,该项目标签的文本大小会增加。如此处的 'tournaments' 标签所示。

有没有办法删除它,使单词 'tournaments' 保持相同大小?

尝试在 dimens.xml 文件中添加此代码

<dimen name="design_bottom_navigation_text_size" tools:override="true">10sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">10sp</dimen>

您可以通过样式为 BottomNavigationView 设置 activeinactive textAppearance:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/BottomNavigationView"/>

将以下样式放入 styles.xml 文件

<style name="BottomNavigationView">
    <item name="itemTextAppearanceActive">@style/TextAppearance.BottomNavigationView.Active</item>
    <item name="itemTextAppearanceInactive">@style/TextAppearance.BottomNavigationView.Inactive</item>
</style>

 <!-- blank styles for better code readability-->
<style name="TextAppearance"/>
<style name="TextAppearance.BottomNavigationView"/>

<!-- inactive tab icon style -->
<style name="TextAppearance.BottomNavigationView.Inactive">
    <item name="android:textSize">12sp</item>
</style>

<!-- active tab icon style -->
<style name="TextAppearance.BottomNavigationView.Active">
    <item name="android:textSize">12sp</item>
</style>

使用 TextAppearance,您不仅可以控制 textSize,还可以控制 fontFamily 等属性

如果您使用支持库“28.0.0-alpha1”或更高版本,您必须做 2 件简单的事情 -

在您的 dimen.xml 文件中添加以下两行

<dimen name="design_bottom_navigation_text_size" tools:override="true">15sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">15sp</dimen>

并且在视图中 -

<android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            android:foreground="?attr/selectableItemBackground"
            app:itemIconTint="@color/colorAccent"
            app:itemTextColor="@color/colorAccent"
            android:elevation="16dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:labelVisibilityMode="labeled"
            app:menu="@menu/navigation" />

app:labelVisibilityMode="labeled"

这就是全部享受:-)

所有控件 UI BottomNavigationView,如下。 我对 BottomNavigationView 使用依赖性 com.google.android.material:material。

 private void editBottomNavigationViewItems(BottomNavigationView bottomNavigationView) {

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

            try {

                View item = bottomNavigationView.getChildAt( i );

                if (item instanceof BottomNavigationMenuView) {

                    BottomNavigationMenuView menu = (BottomNavigationMenuView) item;
                    for (int j = 0; j < menu.getChildCount(); j++) {

                        try {

                            View menuItem = menu.getChildAt( j );

                            // not chosen item menu  GO
                            View _small = menuItem.findViewById(com.google.android.material.R.id.smallLabel);//dependence com.google.android.material:material
                            //View _small = menuItem.findViewById(android.support.design.R.id.smallLabel);// dependence android.support.design
                            if ( _small instanceof TextView ) {
                                //_small.setPadding(0, 0, 0, 0);// remove all padding
                                TextView _tv = (TextView)_small;
                                _tv.setTextSize( 12 );// set size text
                            }// not chosen item menu  END

                            //this chosen item menu GO
                            View _large = menuItem.findViewById(com.google.android.material.R.id.largeLabel);//dependence com.google.android.material:material
                            //View _large = menuItem.findViewById(android.support.design.R.id.largeLabel);//dependence android.support.design.R.id.largeLabel
                            if ( _large instanceof TextView ) {
                                _large.setPadding(0,0,0,0);// remove all padding
                                TextView _tv = (TextView)_large;
                                _tv.setTextSize( 12 );// set size text
                            }// this chosen item menu  END

                        } catch ( NullPointerException npei ) {
                            Log.e("TAG", "get:BottomNavigationMenuView: " + npei.getMessage() );
                        }

                    }

                }

            } catch ( NullPointerException npe ) {
                Log.e("TAG", "get:BottomNavigationView: " + npe.getMessage() );
            }

        }

    }

在onCreate()之前使用此方法bottom_navigation_menu.setOnNavigationItemSelectedListener:-

public void removePaddingFromBottomNavigationItem() {
        BottomNavigationMenuView menuView = (BottomNavigationMenuView) bottom_navigation_menu.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);
            }
        }
    }

并在 styles.xml 中使用此代码:-

<style name="TextAppearance.BottomNavigationView.Inactive">
    <item name="android:textSize">@dimen/_10ssp</item>
</style>

<style name="TextAppearance.BottomNavigationView.Active">
    <item name="android:textSize">@dimen/_10ssp</item>
</style>

尝试使用 dimen.xml

中的以下行
<dimen name="design_bottom_navigation_text_size" tools:override="true">8sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">8sp</dimen>
<dimen name="design_bottom_navigation_icon_size">15dp</dimen>
<dimen name="design_bottom_navigation_height">45dp</dimen>