英文:
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
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.
答案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
<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">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论