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

Posted

技术标签:

【中文标题】底部导航栏:按下时文本大小增加?【英文标题】:Bottom Navigation Bar: Text size is increasing on pressing? 【发布时间】:2018-05-10 16:23:57 【问题描述】:

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

有没有办法删除这个,让“锦标赛”这个词保持相同的大小?

【问题讨论】:

【参考方案1】:

尝试将此代码添加到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>

【讨论】:

我没有收到任何错误日志。我什至尝试在库的 value.xml 文件中更改文本大小的值并将badge_text_size 设置为 50sp。但仍然没有运气。 感谢您的解决方案,它节省了我很多时间!【参考方案2】:

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

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_
    android:layout_
    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 等属性。

【讨论】:

为我工作。非常感谢 引用使用的 attrs:github.com/material-components/material-components-android/blob/…【参考方案3】:

如果您使用支持库 '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_
            android:layout_
            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"

这就是享受:-)

【讨论】:

【参考方案4】:

所有控件 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() );
            

        

    

【讨论】:

这是我的解决方案,遍历每个大标签并删除填充。谢谢!【参考方案5】:

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

【讨论】:

【参考方案6】:

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

【讨论】:

以上是关于底部导航栏:按下时文本大小增加?的主要内容,如果未能解决你的问题,请参考以下文章

移动到其他页面时无法保持底部导航栏颤动

微信开发者工具只显示底部导航栏

按下标签栏项目时如何关闭视图?

鸿蒙系统—打造通用的底部导航栏

Flutter沉浸式状态栏/AppBar导航栏/仿咸鱼底部凸起导航

Flutter:如何在没有android底部导航栏的情况下显示屏幕键盘来聚焦文本字段?