【问题标题】:KOTLIN - First item in BottomNavigationBar isn't visible, title issuesKOTLIN - BottomNavigationBar 中的第一项不可见,标题问题
【发布时间】:2020-03-04 23:30:47
【问题描述】:

我有一个包含 5 个项目的 BottomNavigationBar。除了名称和图标之外,第一项与其余项相同。这是bottom_nav菜单的xml

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

    <item
        android:id="@+id/name_configuration"
        android:title="Name Mech"
        android:icon="@drawable/ic_action_name"
        />

    <item
        android:id="@+id/mech_skills"
        android:title="Skills"
        android:icon="@drawable/ic_action_skills"
        />

    <item
        android:id="@+id/stat_hub"
        android:title="Stat Hub"
        android:icon="@drawable/ic_action_stats"
        />

    <item
        android:id="@+id/mech_weapons"
        android:title="Weapons"
        android:icon="@drawable/ic_action_weapon"
        />

    <item
        android:id="@+id/mech_systems"
        android:title="Systems"
        android:icon="@drawable/ic_action_system"
        />
</menu>

这是我用于图标的drawable的xml

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24"
    android:tint="#FFFFFF"
    android:alpha="0.8">
    <path
        android:fillColor="#FF000000"
        android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>

最后,这是我用于 Bar 本身的 xml

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/btm_nav"
        app:layout_constraintBottom_toBottomOf="parent"
        android:background= "@color/colorPrimary"
        app:menu="@menu/bottom_nav"/>

其余可绘制对象的 xml 完全相同(它们使用不同的图标,但这与 xml 本身无关)。但是,name_configuration 项在底部导航栏中是不可见的。单击时,它的行为与其他项目相同(它们传递 SharedPreferences 值并移动到不同的活动),只是不可见。但是,当按下底部导航栏上的其他按钮时,名称按钮会在应用程序进入下一个活动之前短暂变为正确的颜色/透明度,并且名称按钮再次变得不可见。

此外,Name Mech 的标题是底部导航栏中任何按钮的唯一标题。 “Name Mech”出现在底部导航栏的图标下方,但“Skills”、“StatHub”、“Weapons”和“Systems”并没有出现在底部导航栏各自的图标下方。

任何指导将不胜感激。

【问题讨论】:

    标签: android xml kotlin bottomnavigationview android-bottomnav


    【解决方案1】:

    您的标签被视图隐藏了。您可以在 BottomNavigationView 上设置 app:labelVisibilityMode="labeled" 以强制它们出现。我相信如果它们占用太多空间,文本就会被截断。

    【讨论】:

    • 解决了标题的问题,谢谢!知道是什么导致底部导航栏上的第一项不可见吗?我已经编辑了我的帖子以包含 BottomNavigationView 的 xml
    【解决方案2】:

    当您没有为您的 BottomNavigationView 设置“labelVisibilityMode”时,它默认为自动模式,在此模式下,只有第一个项目被标记,其他项目未标记。当你将'labelVisibilityMode'设置为labeled时,所有的项目都会被标记。

    在默认模式下是:

    app:labelVisibilityMode="auto"
    

    你把它改成:

    app:labelVisibilityMode="labeled"
    

    如果你根本不需要标签,只需设置:

    app:labelVisibilityMode="unlabeled"
    

    【讨论】:

    • 非常感谢。我仍然对第一个项目 name_configuration 有我的问题,完全不可见,图标和标题都是。它在更改活动时短暂出现,但随后又回到不可见状态。对此有何见解?
    【解决方案3】:

    我遇到了同样的问题,但现在我的代码可以正常工作了。试试我的代码:

    在 build.gradle(:app) 中:

    dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
        implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
        implementation 'com.android.support:appcompat-v7:28.0.0'
        implementation 'com.android.support.constraint:constraint-layout:1.1.3'
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'com.android.support.test:runner:1.0.2'
        androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
        implementation 'com.android.support:design:28.0.0'
    }
    

    在 XML 中:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout android:layout_height="match_parent"
        android:layout_width="match_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigationView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:itemBackground="@color/colorPrimary"
            app:itemIconTint="#fff"
            app:itemTextColor="#fff"
            app:menu="@menu/menu"
            app:labelVisibilityMode="unlabeled"/>
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

    • 做到了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2018-02-22
    • 1970-01-01
    • 2012-06-15
    • 1970-01-01
    相关资源
    最近更新 更多