Android导航图标-个人资料图片而不是汉堡包图标 [英] Android Navigation Icon - Profile picture instead of hamburger icon

查看:125
本文介绍了Android导航图标-个人资料图片而不是汉堡包图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找到如何将打开导航抽屉的汉堡包图标更改为一些自定义图片,该图片可以随Picasso或类似图片一起加载.就像上面的图片一样,Twitter设法将我的个人资料图片替换为以前的汉堡图标.

I'm trying to find how to change the hamburger icon that opens the navigation drawer to some custom image that can be loaded with Picasso or something like that. Just like in the image above, where Twitter managed to put my profile picture replacing the hamburger icon they had before.

我在这里找不到这样的东西,我也不知道该怎么做.

I couldn't find anything like this here and I don't have a clue about how to do it.

如果有人获得了样品或有关操作方法的指导,我将不胜感激.

If anyone got a sample or some guidance on how to do it, I would really appreciate it.

编辑

我的一些活动代码:

ActivityMapsBinding binding;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);

    ...

    setSupportActionBar(binding.mapsContent.toolbar);
    getSupportActionBar().setTitle("");

    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, binding.drawerLayout, binding.mapsContent.toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    binding.drawerLayout.addDrawerListener(toggle);

    toggle.syncState();

    binding.navView.setNavigationItemSelectedListener(this);
}


@Override
public void onBackPressed() {
    if (binding.drawerLayout.isDrawerOpen(GravityCompat.START)) {
        binding.drawerLayout.closeDrawer(GravityCompat.START);
    } else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }

    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, R.string.message_press_twice, Toast.LENGTH_SHORT).show();

    new Handler().postDelayed(() -> doubleBackToExitPressedOnce = false, 2000);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()){
        case R.id.nav_profile:
            break;
        case R.id.nav_settings:
            break;
    }

    binding.drawerLayout.closeDrawer(GravityCompat.START);
    return true;
}

编辑2-找到答案

在Bruno Pinto的帮助下进行了此主题的研究和一点点研究之后,我终于使它起作用了. 您需要创建一个Target并将ImageView的标签设置为此Target,并且在加载Picasso时,您引用创建的ImageView.在Tar​​get的构造函数中,将视图替换为已加载的位图.

After the help of Bruno Pinto here in this thread and a little bit of research I finally made it work. You need to create a Target and set the tag of your ImageView as this Target, and when you load with Picasso, you reference this ImageView you created. Inside the Target's constructor you replace your views with the loaded Bitmap.

这是我的做法:

private void changeImageDrawable(){
    ImageView profileImage = (ImageView) binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);

    final Target target = new Target() {
        @Override
        public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
            profileImage.setImageBitmap(bitmap);

            Drawable drawable = new BitmapDrawable(getResources(), bitmap);
            binding.mapsContent.toolbar.setNavigationIcon(drawable);
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    profileImage.setTag(target);

    Picasso.with(this)
            .load("YOUR_IMAGE_URL_HERE")
            .placeholder(R.drawable.placeholder_profile)
            .error(R.drawable.placeholder_profile)
            .into(target);
}

如果发生错误,您可能还应该更改视图可绘制或位图.

You should probably change you views drawable or bitmap in case of error as well.

推荐答案

您可以从工具栏使用 .setNavigationIcon()方法.

类似这样的东西:

((Toolbar) getSupportActionBar()).setNavigationIcon(YOUR_ICON);

这篇关于Android导航图标-个人资料图片而不是汉堡包图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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