BottomNavigationView:设置圆形图像并获取视图参考

问题描述

enter image description here

如您在上图中所看到的,我想在项目上放置一个自定义图像。 我已经尝试了几种解决方案,但无济于事。 第一个问题是有可能放置个性化菜单。 为此,我的菜单是通过以下方式构造的:

profile_image_layout.xml

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true">

        <androidx.cardview.widget.CardView
            android:layout_width="32dp"
            android:layout_height="32dp"
            android:layout_marginStart="@dimen/activity_horizontal_margin"
            android:animateLayoutChanges="true"
            app:cardBackgroundColor="@color/windowBackground"
            app:cardCornerRadius="32dp"
            app:cardElevation="0dp"
            app:cardMaxElevation="0dp">
            <androidx.appcompat.widget.AppCompatimageView
                android:id="@+id/picture_profile"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="false"
                android:animateLayoutChanges="true"
                android:scaleType="fitCenter" />
        </androidx.cardview.widget.CardView>
    </FrameLayout>

bottom_navigation_menu_home.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">

        <item
            android:id="@id/contacts"
            android:icon="@drawable/contacts"
            android:title="@string/contacts"
            app:showAsAction="always|withText" />

        <item
            android:id="@id/calendar"
            android:icon="@drawable/calendar"
            android:title="@string/calendar"
            app:showAsAction="always|withText" />

        <item
            android:id="@id/events"
            android:icon="@drawable/events"
            android:title="@string/events"
            app:showAsAction="always|withText" />

        <item
            android:id="@id/groups"
            android:icon="@drawable/groups"
            android:title="@string/groups"
            app:showAsAction="always|withText" />

        <item
            android:id="@id/profile"
            android:icon="@drawable/profile"
            android:title="@string/profile"
            android:actionLayout="@layout/profile_image_layout"
            app:actionViewClass="android.widget.FrameLayout"
            app:showAsAction="always|withText" />

    </menu>

此后,我的目标是将感兴趣的图像放入“个人资料”项中,尤其是在profile_image_layout:“ picture_profile”菜单中。 要恢复我尝试过的视图:

    FrameLayout rootView = (FrameLayout) navigator.getMenu().findItem(R.id.profile).getActionView();

但是从rootView我无法访问“ picture_profile”。谁能告诉我我做得好还是做错了什么? 预先谢谢你

解决方法

您可以使用本地存储库(SingleTon设计模式),将登录的个人资料信息保存在其中,并从中读取个人资料图片和信息

要使用存储库模式,您需要具有一个包含必填字段的单例类 我在下面为您留了一些链接

单个设计模式:https://refactoring.guru/design-patterns/singleton

这是我旧程序中的示例代码(请查看存储库包装,这是我的第一个程序,对此并不十分抱歉):https://github.com/ErfanDP/TicTacToe_and_4_in_Row

,

您的menu.xml中有两个错误:

            <item
            android:id="@id/profile"
            android:icon="@drawable/profile"
            android:title="@string/profile"
            android:actionLayout="@layout/profile_image_layout"
            app:actionViewClass="android.widget.FrameLayout"
            app:showAsAction="always|withText" />
  1. android:actionLayout =“ @ layout / profile_image_layout”。应该是app:actionLayout =“ @ layout / profile_image_layout”。
  2. 您应仅使用“ actionLayout”和“ actionViewClass”之一,否则将选择“ actionViewClass”。删除“ app:actionViewClass”后,它可以正常工作。