如何将底部导航视图颜色更改为使用 xml 创建的自定义颜色?

塞缪尔

是否可以将底部导航视图颜色更改为使用 xml 创建的自定义颜色?如果是这样,我该怎么做?我知道您可以使用属性“app:itemIconTint="@color/colorPrimaryDark”更改图标颜色,但我不确定如何在我的 drawable 中为 iconTint 颜色引用颜色。

底部导航视图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.examp.smartshop.Controller.MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation">

    </FrameLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@color/colorPrimaryDark"
        app:itemTextColor="@color/colorPrimaryDark"
        app:menu="@menu/bottom_navigation"
        app:itemIconSize="25sp"

        android:background="?android:attr/windowBackground"/>

</RelativeLayout>

**Custom color**
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >

    <gradient
        android:angle="90"
        android:endColor="#EC6EAD"
        android:startColor="#3494E6"
        android:type="linear" />

    <corners
        android:radius="0dp"/>

</shape>
贾斯帕辛戈希尔

Android 底部导航视图

禁用此行为

bottomNavigationView.itemIconTintList = null

**

更改图标和选定的图标

**

导航项(包括文本和图标)在菜单文件中指定。

<com.google.android.material.bottomnavigation.BottomNavigationView
        ...
        app:menu="@menu/navigation"
        />



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

    <item
        android:id="@+id/navigation_dailyquote"
        android:icon="@drawable/yy_dailyquote"
        android:title="@string/navigation_dailyquote"/>

        ...
</menu>

要同时支持图标和选定图标,请创建 res/drawable/yy_dailyquote.xml。

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

TextColor 和选定的 TextColor

更改文本颜色

<com.google.android.material.bottomnavigation.BottomNavigationView
        ...
        app:itemTextColor="#998971"
        />

支持 TextColor 和 Selected TextColor

<com.google.android.material.bottomnavigation.BottomNavigationView
        ...
        app:itemTextColor="@color/navigation_text_color"
        />

创建 res/color/navigation_text_color。

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

更改背景颜色

<com.google.android.material.bottomnavigation.BottomNavigationView
        ...
        android:background="#FDFAE6"
        />

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章