使底部导航视图背景透明

Make a Bottom Navigation Views background transparent

所以我在我的应用程序中添加了一个底部导航视图,但是有一个问题。我希望我的底部导航视图的背景是透明的(它目前是黑色的)即我希望它显示它覆盖的 Relative 布局(包含 btmnav )的背景部分。我在某处读到它这样做默认情况下,我需要做的就是给相对布局一个背景,它会起作用...但是当我这样做时,底部导航视图的背景仍然是黑色。

菜单资源文件:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:id="@+id/live"
    android:icon="@drawable/ic_live_tv_black_24dp"
    android:title="Live"/>

<item
    android:id="@+id/squad"
    android:icon="@drawable/ic_team_squad_black_24dp"
    android:title="Squad"/>

<item
    android:id="@+id/schedule"
    android:icon="@drawable/ic_schedule_black_24dp"
    android:title="Schedule"/>
<item
    android:id="@+id/feed"
    android:icon="@drawable/ic_rss_feed_black_24dp"
    android:title="Feed"/>

 </menu>

布局文件中的代码

   <?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:id="@+id/btmnav"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    app:itemTextColor="#fff3CD70"
    app:itemIconTint="#fff3CD70"


    app:menu="@menu/bottom_navigation" />



    </RelativeLayout>

Before adding the background

After adding the background

What i want

将 'btmnav' 的背景设置为具有透明 alpha 通道的颜色。

添加到您的 BottonNavigationView android:background="#00ffffff"

您还可以设置主题并在那里设置:

android:theme="@style/NavigationTheme"

Styles.xml

<style name="NavigationTheme" parent="AppTheme">
    <item name="itemIconTint">@android:color/white</item>
    <item name="itemTextColor">@android:color/white</item>
    <item name="android:background">#00ffffff</item>
</style>

我正在使用这条线:

BottomNavigationView navView = findViewById(R.id.nav_view);
navView.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), android.R.color.transparent));

这是我的project

步骤 1:创建一个带有透明矩形的可绘制文件

res\drawable\transparent_rect.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#00000000" />
</shape>

第 2 步: 使用 android:background

BottomNavigationView 的背景设置为此可绘制对象
android:background="@drawable/transparent_rect"