How to remove the background color of icons in BottomNavigationView and make the text color white when it is active in android studio flamigo?

huangapple go评论120阅读模式
英文:

How to remove the background color of icons in BottomNavigationView and make the text color white when it is active in android studio flamigo?

问题

这是我的代码部分的翻译:

activity_main.xml

<?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=".MainActivity">
    
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/settingBg"
        app:menu="@menu/navigation_items"
        app:itemRippleColor="@color/white"
        app:itemIconTint="@color/icon_color"/>

</RelativeLayout>

res/color/icon_color.xml

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

navigation_items.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:title="Drive"
        android:id="@+id/navDrive"
        android:icon="@drawable/baseline_drive_eta_24"
        />
    <item
        android:title="LIGHT"
        android:id="@+id/navLight"
        android:icon="@drawable/baseline_flashlight_on_24"
        />
    <item
        android:title="DASH"
        android:id="@+id/navDash"
        android:icon="@drawable/baseline_dashboard_customize_24"
        />
    <item
        android:title="Settings"
        android:id="@+id/navSettings"
        android:icon="@drawable/baseline_settings_24"
        />
</menu>

res/themes/thems.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.BottomNavigationBar" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.BottomNavigationBar" parent="Base.Theme.BottomNavigationBar" />
</resources>

res/themes/thems.xml (night)

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.BottomNavigationBar" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your dark theme here. -->
        <!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
    </style>
</resources>

希望这些帮助你。

英文:

Just started creating a bottom navigation view from scratch using some tutorials, But my navigation seems to look different maybe because of the difference in versions of android studio or dependencies.

These are my codes so far...

activity_main.xml

<?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=".MainActivity">
    
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/settingBg"
        app:menu="@menu/navigation_items"
        app:itemRippleColor="@color/white"
        app:itemIconTint="@color/icon_color"/>

</RelativeLayout>

res/color/icon_color.xml

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

navigation_items.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:title="Drive"
        android:id="@+id/navDrive"
        android:icon="@drawable/baseline_drive_eta_24"
        />
    <item
        android:title="LIGHT"
        android:id="@+id/navLight"
        android:icon="@drawable/baseline_flashlight_on_24"
        />
    <item
        android:title="DASH"
        android:id="@+id/navDash"
        android:icon="@drawable/baseline_dashboard_customize_24"
        />
    <item
        android:title="Settings"
        android:id="@+id/navSettings"
        android:icon="@drawable/baseline_settings_24"
        />
</menu>

res/themes/thems.xml

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.BottomNavigationBar" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your light theme here. -->
        <!-- <item name="colorPrimary">@color/my_light_primary</item> -->
    </style>

    <style name="Theme.BottomNavigationBar" parent="Base.Theme.BottomNavigationBar" />
</resources>

res/themes/thems.xml (night)

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.BottomNavigationBar" parent="Theme.Material3.DayNight.NoActionBar">
        <!-- Customize your dark theme here. -->
        <!-- <item name="colorPrimary">@color/my_dark_primary</item> -->
    </style>
</resources>

My bottom navigation view looks like this
How to remove the background color of icons in BottomNavigationView and make the text color white when it is active in android studio flamigo?

What I wanted was to remove the white background of the icon and also make the text white if it is active or selected like in this tutorial that i found on youtube. How to remove the background color of icons in BottomNavigationView and make the text color white when it is active in android studio flamigo?

答案1

得分: 0

对于那些可能想知道为什么看起来那样的人,我发现其实很简单 - 一切都归结于导航样式。您可以选择在theme.xml中自定义样式,或者您可以简单地使用MaterialComponents的样式,就像这样:

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/black"
        style="@style/Widget.MaterialComponents.BottomNavigationView.PrimarySurface"
        app:layout_constraintBottom_toBottomOf="parent"
        app:menu="@menu/menu">
英文:

For those who may be wondering why it appears that way, I discovered that it's actually quite simple – it all boils down to the navigation style. You have the option to customize the styles yourself in the theme.xml, or you can simply utilize the style from MaterialComponents, like this

&lt;com.google.android.material.bottomnavigation.BottomNavigationView
        android:id=&quot;@+id/bottomNavigationView&quot;
        android:layout_width=&quot;match_parent&quot;
        android:layout_height=&quot;wrap_content&quot;
        android:background=&quot;@color/black&quot;
        style=&quot;@style/Widget.MaterialComponents.BottomNavigationView.PrimarySurface&quot;
        app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
        app:menu=&quot;@menu/menu&quot;&gt;

huangapple
  • 本文由 发表于 2023年7月18日 09:26:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709002.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定