如何动态更改自定义工具栏图标 [英] How can I change my custom toolbar icon dynamically

查看:112
本文介绍了如何动态更改自定义工具栏图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用的是toolbar,其中包含imageview.我需要动态更改该imageview.

In my application I'm using toolbar which having imageview in it. I need to change that imageview dynamically .

这是我的 toolbar.xml :

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    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="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/white"
    android:id="@+id/toolbar"
    app:theme="@style/ToolbarStyle" >

    <ImageView
        android:id="@+id/actionBarImage"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginRight="65dp"
        android:src="@drawable/forewarn"/>

</android.support.v7.widget.Toolbar>

以下是活动中的工具栏声明:

Here is toolbar declaration in activity:

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
final ImageView imageInToolbar = (ImageView) toolbar.findViewById(R.id.actionBarImage);
setSupportActionBar(toolbar);
final android.support.v7.app.ActionBar actionBar = getSupportActionBar();

        if (actionBar != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//            getSupportActionBar().setLogo(R.drawable.abc);
        }

这是我的实际工具栏:

我需要的具有动态更改imageview的工具栏:

my required toolbar with dynamic change of imageview :

推荐答案

要从服务器加载图像:

对于 Glide 使用:

Glide.with(this)
     .load(url) 
     .into(imageInToolbar);

在gradle中:compile 'com.github.bumptech.glide:glide:3.8.0'

对于毕加索,请使用:

Picasso.with(this).load(url).into(imageInToolbar);

这篇关于如何动态更改自定义工具栏图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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