使底部导航视图背景透明

时间:2020-05-11 16:47:11

标签: android androidx bottomnavigationview

所以我已经在我的应用程序中添加了底部导航视图,但是有一个问题。我希望我的底部导航视图的背景是透明的(当前是黑色的),即我希望它显示相对布局(包含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

3 个答案:

答案 0 :(得分:2)

我正在使用此行:

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

这是我的project

答案 1 :(得分:1)

将“ 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>

答案 2 :(得分:0)

步骤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:使用BottomNavigationView

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