从某些网址下载的BottomNavigationView设置自定义图标 [英] BottomNavigationView set custom icon downloaded from some url

查看:75
本文介绍了从某些网址下载的BottomNavigationView设置自定义图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在屏幕底部有4个标签,对于最后一个标签,我想设置一个用户个人资料图片(如果有的话).我尝试了一切.仅可绘制对象中的图标集可以正常工作.其他所有情况都是灾难.

I have 4 tabs on the bottom of the screen and for the last tab I want to set a user profile image if he has it. I tried everything. Only the icon set from a drawable works fine. Every other case is a disaster.

我发现,如果我在高于26的android版本上删除图标色调和图标模式,则可以正常工作.对于其中的版本,它不起作用.

I found out that if I remove icon tint and icon mode on android versions above 26 it works fine. For versions under it, it's not working.

这是代码.也许有人会对如何提供帮助有一些想法. 这就是我从可绘制对象设置图标的方式.此imageView只是一项测试,以确保从服务器下载了图像.

Here is the code. Maybe somebody will have some ideas about how to help. This is how i set the icon from a drawable. This imageView is just a test to be sure that image is downloaded from the server.

@Override
public void loadUrlAsTabProfileImage(String url) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        navigationView.getMenu().getItem(presenter.getProfileTabPosition()).setIconTintList(null);
        navigationView.getMenu().getItem(presenter.getProfileTabPosition()).setIconTintMode(null);
    }
    Glide.with(HomeActivity.this)
            .asBitmap()
            .load(url)
            .apply(RequestOptions.circleCropTransform())
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    Drawable drawable = new BitmapDrawable(getResources(), resource);
                    imageView.setImageDrawable(drawable);
                    imageView.setVisibility(View.VISIBLE);

                    navigationView.getMenu().findItem(R.id.layout_profile).setIcon(drawable);
                }
            });
}

这是xml中菜单的样子

This is how menu looks like in xml

<item android:id="@+id/layout_feed"
    android:title="@string/feed"
    android:icon="@drawable/ic_feed_bottombar"
    app:showAsAction="always"/>

<item android:id="@+id/layout_inbox"
    android:title="@string/inbox"
    android:icon="@drawable/ic_inbox_bottombar"
    app:showAsAction="always"/>

<item android:id="@+id/layout_contacts"
    android:title="@string/contacts"
    android:icon="@drawable/ic_contacts_bottombar"
    app:showAsAction="always"/>

<item android:id="@+id/layout_profile"
    android:title="@string/profile"
    android:icon="@drawable/ic_profile_bottombar"
    app:showAsAction="always"/>

我尝试使用标签app:actionViewClass ="设置自定义操作布局,但是没有运气.动作类中的ImageView根本不可见.

I tried with setting custom action layout with a tag app:actionViewClass="" but no luck. ImageView from the action class is not visible at all.

这是我的布局外观.

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<androidx.viewpager.widget.ViewPager
    android:id="@+id/homePager"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@id/navigation_bar"/>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/plus_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="end"
    android:layout_margin="16dp"
    android:backgroundTint="@color/bottombar_blue_color"
    android:foregroundGravity="bottom"
    android:tint="@color/white"
    app:srcCompat="@drawable/plus_icon"
    app:tint="@color/white"
    app:borderWidth="0dp"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@id/navigation_bar"/>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/navigation_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@color/navigation_bar_background"
    app:itemIconTint="@color/navigation_bar_background"
    app:labelVisibilityMode="labeled"
    app:layout_constraintBottom_toBottomOf="parent"
    />

<ImageView
    android:id="@+id/image"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    android:visibility="gone"/>

推荐答案

如果您仍在寻找答案,这是建议

If you are still looking for answer here is the suggestion

为已选择和取消选择图像的每个项目创建可绘制选择器. 也为配置文件创建选择器.

create drawable selector for each item which have selected and deselected image. create selector for profile also.

<item android:id="@+id/layout_feed"
    android:title="@string/feed"
    android:icon="@drawable/feed_tab_color_selector"
    app:showAsAction="always"/>

feed_tab_color_selector:

feed_tab_color_selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/feed_selected" android:state_selected="true" />
    <item android:drawable="@drawable/feed_normal" android:state_focused="false" android:state_pressed="false" android:state_selected="false" />
</selector>

然后在您的活动中应用此代码

after that apply this code in your activity

public void loadUrlAsTabProfileImage(String url) {

bottomMenu.seIitemIconTintList(null) // this is important

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        navigationView.getMenu().getItem(presenter.getProfileTabPosition()).setIconTintList(null);
        navigationView.getMenu().getItem(presenter.getProfileTabPosition()).setIconTintMode(null);
    }
    Glide.with(HomeActivity.this)
            .asBitmap()
            .load(url)
            .apply(RequestOptions.circleCropTransform())
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    Drawable drawable = new BitmapDrawable(getResources(), resource);
                    imageView.setImageDrawable(drawable);
                    imageView.setVisibility(View.VISIBLE);

                    navigationView.getMenu().findItem(R.id.layout_profile).setIcon(drawable);
                }
            });
}

请尝试此操作,这将解决您的问题

Please try this this will resolve your problem

这篇关于从某些网址下载的BottomNavigationView设置自定义图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆