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

时间:2017-11-27 12:15:38

标签: java android material-design bottomnavigationview

我在Android中使用底部导航栏。默认情况下,当我选择项目时,该项目标签的文本大小会增加。正如“锦标赛”标签所示。

enter image description here

enter image description here

有没有办法删除它,所以“锦标赛”这个词的大小保持不变?

6 个答案:

答案 0 :(得分:13)

尝试在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>

答案 1 :(得分:12)

您可以通过样式为BottomNavigationView设置有效无效文本外观:

<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等属性。

答案 2 :(得分:0)

如果使用支持库' 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"

非常享受:-)

答案 3 :(得分:0)

下面的所有控件UI BottomNavigationView。 我将com.google.android.material:material依赖项用于BottomNavigationView。

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

        }

    }

答案 4 :(得分:0)

在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>

答案 5 :(得分:0)

尝试使用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>