简体   繁体   English

为什么我的底部导航视图不显示任何标题或图标?

[英]Why my Bottom Navigation View does not show any titles or icons?

Here is my Bottom Navigation View in activity_main.xml:这是我在 activity_main.xml 中的底部导航视图:

    <com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:background="?android:attr/windowBackground"
android:backgroundTint="#1091BF"
app:itemIconTint="#fff"
app:itemTextColor="#fff"
app:menu="@menu/navigation_bottom" />

Here is my Bottom Navigation Menu in navigation_bottom.xml:这是我在 navigation_bottom.xml 中的底部导航菜单:

<?xml version="1.0" encoding="utf-8"?>

<item
    android:id="nav_home"
    android:icon="@drawable/ic_navigation_24dp"
    android:title="@string/home"/>

<item
    android:id="nav_education"
    android:icon="@drawable/ic_school_24dp"
    android:title="@string/education"/>

<item
    android:id="nav_report"
    android:icon="@drawable/ic_report_24dp"
    android:title="@string/report"/>

<item
    android:id="nav_activity"
    android:icon="@drawable/ic_activity_24dp"
    android:title="@string/activity"/>

<item
    android:id="nav_profile"
    android:icon="@drawable/ic_person_24dp"
    android:title="@string/profile"/>

Here is my dependencies in the build.gradle(Module:app):这是我在 build.gradle(Module:app) 中的依赖项:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

//Added code
//Navigation Dependencies
def nav_version = "2.1.0"
implementation "androidx.navigation:navigation-fragment:$nav_version"
implementation "androidx.navigation:navigation-ui:$nav_version"}

I don't understand why my Bottom Navigation View doesn't show anything.我不明白为什么我的底部导航视图不显示任何内容。 On the design tab in activity_main.xml, it doesn't even have any height when I choose wrap_content to the layout_height.在activity_main.xml 的设计选项卡上,当我选择wrap_content 到layout_height 时,它甚至没有任何高度。

请参阅: LabelVisibilityMode将您的 app:labelVisibilityMode 设置为已标记

If you want to use BottomNavigation you can set you menu in you xml file with:如果你想使用 BottomNavigation 你可以在你的 xml 文件中设置你的菜单:

app:menu="@menu/my_navigation_items"

All you have do then is override the onOptionsItemSelected(MenuItem item) method in you activity and implement the real navigation.您所要做的就是覆盖您活动中的onOptionsItemSelected(MenuItem item)方法并实现真正的导航。

If I get you question right you also want to display a title?如果我问对了,您还想显示标题吗? The name of you App?你的应用程序名称? You can't do that with BottomNavigation.你不能用 BottomNavigation 做到这一点。 You should then use an AppBarLayout together with your BottomNavigation然后,您应该将 AppBarLayout 与 BottomNavigation 一起使用

I hope this will help you.我希望这能帮到您。


Source:来源:

Try to use app1 attributes instead of app:尝试使用 app1 属性而不是 app:

app1:itemIconTint="#fff"
app1:itemTextColor="#fff"
app1:menu="@menu/navigation_bottom"

Also, add this dependency in your Module:app version of gradle.此外,在您的 Module:app 版本的 gradle 中添加此依赖项。

implementation 'com.google.android.material:material:1.2.0'

Hope it works!!!希望它有效!!!

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

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