BottomNavigation所选图标未显示?

时间:2019-04-19 07:42:09

标签: android android-layout kotlin bottomnavigationview

我使用底部导航视图显示菜单。 当我运行该应用程序时,它没有显示所选的图标和文本,请参见下图。(应用程序使用svg(xml)图像文件)

当TextSize的长度更大且制表符大于3或4时进行TextSize切割 See this image for text cutting

enter image description here

这里是我的代码 MainActivity xml文件

 <android.support.design.widget.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="0dp"
            android:layout_marginStart="0dp"
            app:labelVisibilityMode="labeled"
            android:background="?android:attr/windowBackground"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:menu="@menu/navigation"/>

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

导航菜单

<item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home_light"
        android:title="@string/str_home"/>

<item
        android:id="@+id/navigation_medidate"
        android:icon="@drawable/ic_meditation_light"
        android:title="@string/str_meditade"/>

<item
        android:id="@+id/navigation_lessions"
        android:icon="@drawable/ic_lesson_light"
        android:title="@string/str_lessions"/>

<item
        android:id="@+id/navigation_sleep"
        android:visible="false"
        android:icon="@drawable/ic_sleep_light"
        android:title="@string/str_sleep"/>

<item
        android:id="@+id/navigation_cources"
        android:icon="@drawable/ic_course_light"
        android:visible="false"
        android:title="@string/str_courses"/>

导航监听器

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_home -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_cources -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_lessions -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_medidate -> {
                return@OnNavigationItemSelectedListener true
            }
            R.id.navigation_sleep -> {
                return@OnNavigationItemSelectedListener true
            }
        }
        false
    }

3 个答案:

答案 0 :(得分:0)

尝试为色调图标selector_navigation.xml添加选择器。使用app:itemIconTint属性。

<?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/blue" />
<item android:color="@android:color/darker_gray"  />
</selector>

并设置为BottomNavigationView

 <android.support.design.widget.BottomNavigationView
                android:id="@+id/navigation"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="start"
                android:elevation="4dp"
                android:background="@color/white"
                app:itemTextColor="@color/darker_gray"
                app:menu="@menu/navigation"
                app:layout_behavior="BottomNavigationBehavior"
                app:itemIconTint="@drawable/selector_navigation" />

答案 1 :(得分:0)

您可以修改选定或未选定标签的文本大小。在dimens.xml中添加以下代码

    <dimen name="design_bottom_navigation_text_size">11sp</dimen>
    <dimen name="design_bottom_navigation_active_text_size">12sp</dimen>

以下其他自定义尺寸的代码

    <dimen name="design_bottom_navigation_active_item_max_width">168dp</dimen>
    <dimen name="design_bottom_navigation_active_item_min_width">96dp</dimen>       
    <dimen name="design_bottom_navigation_elevation">8dp</dimen>
    <dimen name="design_bottom_navigation_height">56dp</dimen>
    <dimen name="design_bottom_navigation_icon_size">20dp</dimen>
    <dimen name="design_bottom_navigation_item_max_width">96dp</dimen>
    <dimen name="design_bottom_navigation_item_min_width">56dp</dimen>
    <dimen name="design_bottom_navigation_margin">8dp</dimen>
    <dimen name="design_bottom_navigation_shadow_height">1dp</dimen>
    <color name="design_bottom_navigation_shadow_color">#14000000</color>

答案 2 :(得分:0)

需要在onNavigation点击列表器上返回“ true”而不是false

private val mOnNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
    when (item.itemId) {
        R.id.navigation_home -> {
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_cources -> {
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_lessions -> {
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_medidate -> {
            return@OnNavigationItemSelectedListener true
        }
        R.id.navigation_sleep -> {
            return@OnNavigationItemSelectedListener true
        }
    }
    true
}