简体   繁体   English

底部导航查看所选项目上的文本样式

[英]Bottom Navigation View text style on selected item

How do I apply text style on selected item in BottomNavigationView?如何在BottomNavigationView中的选定项目上应用文本样式 I can change colours but cannot figure out on how to change font styles (like font family or make it bold/italic) on selected menu item.我可以更改颜色,但无法弄清楚如何更改所选菜单项上的字体样式(如字体系列或使其加粗/斜体)。 Is there a way to do this only with XML?有没有办法只用 XML 来做到这一点?

底部导航视图

In the sample picture above I want only SEARCH to be in different font and bold.在上面的示例图片中,我只希望 SEARCH 使用不同的字体和粗体。 MESSAGE and DASHBOARD remain unchanged. MESSAGE 和 DASHBOARD 保持不变。

in activity xml:在活动 xml 中:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/navigation"
    android:layout_width="0dp"
    android:layout_height="49dp"
    android:background="?android:attr/windowBackground"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    app:itemIconTint="@color/bottom_nav_color"
    app:itemTextColor="@color/bottom_nav_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/navigation"/>

res/menu/navigation.xml res/menu/navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_search"
        android:title="SEARCH" />

    <item
        android:id="@+id/navigation_messages"
        android:icon="@drawable/ic_question_answer"
        android:title="MESSAGES" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_dashboard_black_24dp"
        android:title="DASHBOARD" />
</menu>

res/color/bottom_nav_color.xml res/color/bottom_nav_color.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="true" android:color="@color/colorPrimary" />
    <item android:state_checked="false" android:color="#666666"/>
</selector>

There are attributes called itemTextAppearanceActive and itemTextAppearanceInactive in BottomNavigationView.在BottomNavigationView 中有名为itemTextAppearanceActive 和itemTextAppearanceInactive 的属性。 Just create a style in res/values/styles.xml:只需在 res/values/styles.xml 中创建一个样式:

<style name="BottomNavigation.ActiveItemTextAppearance" parent="TextAppearance.AppCompat">
    <item name="android:textStyle">bold</item>
    <item name="android:fontFamily">@font/whatever</item>
</style>

Then reference it in BottomNavigationView:然后在BottomNavigationView中引用它:

app:itemTextAppearanceActive="@style/BottomNavigation.ActiveItemTextAppearance"

And vice versa for inactive items.对于非活动项目,反之亦然。

I think it worth to mention here that by changing text style, You won't change text color.我认为这里值得一提的是,通过更改文本样式,您不会更改文本颜色。 Therefore it will still require to use app:itemTextColor因此它仍然需要使用app:itemTextColor

<com.google.android.material.bottomnavigation.BottomNavigationView
...
        app:itemIconTint="@color/bottom_nav_color_selector"
        app:itemTextColor="@color/bottom_nav_color_selector"
        app:itemTextAppearanceInactive="@style/BottomNavigationText"
        app:itemTextAppearanceActive="@style/BottomNavigationText.Selected"
        tools:menu="@menu/menu_bottom_navigation"
        />

bottom_nav_color_selector in res/colors bottom_nav_color_selector res/colors bottom_nav_color_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/bottom_nav_selected" android:state_checked="true" />
    <item android:color="@color/bottom_nav_normal" />
</selector>

Styles样式

<style name="BottomNavigationText" parent="@android:style/TextAppearance"/>
<style name="BottomNavigationText.Selected">
    <item name="android:textStyle">bold</item>
</style>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM