自己的自定义图标没有显示在底部导航栏android工作室



我正在尝试制作一个底部导航栏,中间的图标是我自己的图标,应该可以点击并适合像这个

在菜单目录中,我制作了bottom_navigation.xml,其中负责我想要的图标的行是:

<item
android:id="@+id/park"
android:icon="@drawable/ic_parkcenter"
android:title="Park here"/>

在我的maps_activity.xml(我想要显示的主页(中,我使用以下内容:

<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_height="75dp"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:id="@+id/bottom_nav"
map:menu="@menu/bottom_navigation" />

这就是我得到的结果

我想把它作为图像视图而不是一个项目,但遗憾的是,它在菜单中不起作用。我该如何处理?提前感谢!

编辑:my-activity_maps.xml,

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Can be ignored -->
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<!-- Can be ignored -->
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/purple_500"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
map:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_left"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_gravity="bottom"
map:menu="@menu/bottom_navigation"
android:background="@color/purple_500"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom">
<!-- For the image i want in the middle -->
<ImageView
android:id="@+id/parkcenter"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/ic_parkcenter" />
</LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_nav_right"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:layout_gravity="bottom"
map:menu="@menu/bottom_navigation"
android:background="@color/purple_500"/>
<!-- Can be ignored -->
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/parking"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|center"
android:layout_margin="16dp"
android:contentDescription="@string/parking"
android:src="@drawable/ic_parking" />
</FrameLayout>
</LinearLayout>

如果有帮助的话,这一切都在抽屉里!再次感谢!

只需在您的活动或片段中尝试以下java代码:

bottomNavigationView.setItemIconTintList(null) ;

使用bottomNavigationView时使用您的id初始化它。

有更好的方法可以做到这一点,但实现这样的目标的一种巧妙方法是

<LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_height="75dp"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:id="@+id/bottom_nav_left"
map:menu="@menu/bottom_navigation" />

<LinearLayout
android:layout_height="100dp"
android:layout_width="match_parent"
android:layout_gravity="bottom">
<ImageView
android:layout_height="100dp"
android:layout_width="100dp"
src = ... />
//Add textview or any other view. Otherwise remove linearlayout
</LinearLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_height="75dp"
android:layout_gravity="bottom"
android:layout_width="wrap_content"
android:id="@+id/bottom_nav_right"
map:menu="@menu/bottom_navigation" />
</LinearLayout>

这将为您在显示图像时提供额外的灵活性(使中间的图像更大,而不从底部导航进行色调处理(。对于底部导航左侧,填充带有图像左侧图标的项目,对于底部导航右侧也是如此

所以我终于能够弄清楚该做什么,制作一个框架布局,并在其中添加底部导航,其中有5个项目,中间项目没有名称,没有图标,可检查为false,最后在框架布局中添加一个图像视图,并将其放置在中间图标上。

<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:id="@+id/bottom_nav"
android:layout_height="wrap_content"
android:clipChildren="false"
android:layout_gravity="bottom"
android:elevation="15dp"
map:itemIconSize="34dp"
android:background="@color/purple_500"
map:menu="@menu/bottom_navigation"
android:paddingLeft="5dp"
map:itemIconTint="@drawable/select"
map:itemTextColor="@drawable/select"
android:focusableInTouchMode="true"
/>
<ImageView
android:layout_width="wrap_content"
android:id="@+id/tofront"
android:layout_height="wrap_content"
android:src="@drawable/ic_parkcenter"
android:layout_gravity="center"
android:scaleType="center"
android:elevation="@android:dimen/app_icon_size"/>
</FrameLayout>

你可以使用

val front = findViewById<ImageView>(R.id.tofront)
front.bringToFront()

将图像放在前面。一旦你在图像中添加了一个监听器,它就会按预期工作!

最新更新