具有不透明度的 BottomNavigationView 背景颜色无法正常工作
BottomNavigationView background color with opacity not working properly
我已经实现了带导航图的 BottomNavigationView。
在 navigationView 中设置纯色作为背景时,它工作正常,但是当我设置不透明度的背景色时,它无法正常工作。
这是我的 gradle 文件:
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.1'
Navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_nav"
app:startDestination="@id/firstBottomNavFragment">
<fragment
android:id="@+id/firstBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.HomeFragment"
android:label="@string/home" />
<fragment
android:id="@+id/secondBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.ProgramsFragment"
android:label="@string/programs" />
<fragment
android:id="@+id/thirdBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.TrackingFragment"
android:label="@string/tracking" />
<fragment
android:id="@+id/fourBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.MoreFragment"
android:label="@string/more" />
</navigation>
我的主屏幕xml:
<layout 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"
tools:context=".screens.home.HomeScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_5"
app:labelVisibilityMode="labeled"
app:menu="@menu/menu" />
</LinearLayout>
</layout>
我正在使用这段代码来设置 navView 的透明背景。您可以更改颜色并使用:
navView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));
您可以使用任何可绘制文件作为背景,因此它也有助于代码的可重用性,最重要的是,您还可以在应用编译后更改它,就像我们在主题更改中所做的那样
任何希望导航栏透明且可点击的人
android:background="?android:attr/windowBackground"
或
android:background="?android:selectableItemBackground"
试试上面的方法之一
我已经实现了带导航图的 BottomNavigationView。
在 navigationView 中设置纯色作为背景时,它工作正常,但是当我设置不透明度的背景色时,它无法正常工作。
这是我的 gradle 文件:
implementation 'androidx.navigation:navigation-fragment-ktx:2.2.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.2.1'
Navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_nav"
app:startDestination="@id/firstBottomNavFragment">
<fragment
android:id="@+id/firstBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.HomeFragment"
android:label="@string/home" />
<fragment
android:id="@+id/secondBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.ProgramsFragment"
android:label="@string/programs" />
<fragment
android:id="@+id/thirdBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.TrackingFragment"
android:label="@string/tracking" />
<fragment
android:id="@+id/fourBottomNavFragment"
android:name="com.healthymale.screens.home.fragment.MoreFragment"
android:label="@string/more" />
</navigation>
我的主屏幕xml:
<layout 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"
tools:context=".screens.home.HomeScreen">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:defaultNavHost="true"
app:navGraph="@navigation/navigation" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black_5"
app:labelVisibilityMode="labeled"
app:menu="@menu/menu" />
</LinearLayout>
</layout>
我正在使用这段代码来设置 navView 的透明背景。您可以更改颜色并使用:
navView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));
您可以使用任何可绘制文件作为背景,因此它也有助于代码的可重用性,最重要的是,您还可以在应用编译后更改它,就像我们在主题更改中所做的那样
任何希望导航栏透明且可点击的人
android:background="?android:attr/windowBackground"
或
android:background="?android:selectableItemBackground"
试试上面的方法之一