如何在操作栏中正确设置图像和文本?

How to properly set an image and a text in the action bar?

我正在尝试在操作栏中设置用户的图像和名称,如下所示:

我没有使用工具栏。代码:

if (receiverImageURL == null || receiverImageURL.trim().isEmpty()) {
    final Bitmap circleLetterTile = tileProvider.getCircularLetterTile(receiverFullName);
    getSupportActionBar().setIcon(new BitmapDrawable(getResources(), circleLetterTile));
    setTitle(" " +StringUtils.capitalizeFully(receiverFullName));
} else {
    Glide.with(this).asBitmap().load(receiverImageURL).into(new CustomViewTarget<ImageView, Bitmap>(???????) {

        @Override
        protected void onResourceCleared(@Nullable Drawable placeholder) {

        }

        @Override
        public void onLoadFailed(@Nullable Drawable errorDrawable) {
            final Bitmap circleLetterTile = tileProvider.getCircularLetterTile(receiverFullName);
            getSupportActionBar().setIcon(new BitmapDrawable(getResources(), circleLetterTile));
            setTitle(" " +StringUtils.capitalizeFully(receiverFullName));
        }

        @Override
        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
            final Bitmap circleAvatarBitmap = tileProvider.getCircularImage(resource);
            getSupportActionBar().setIcon(new BitmapDrawable(getResources(), circleAvatarBitmap));
            setTitle(" " +StringUtils.capitalizeFully(receiverFullName));
        }
    });
}

如果用户没有图像(第一个 if 语句),这会起作用。但我有以下问题:

  1. 使用 Glide 时,我需要将 ImageView id 传递到 ????? 位置,但我不确定如何获取。
  2. 我真的不喜欢我在 setTitle(" " +StringUtils.capitalizeFully(receiverFullName)); 中用来模拟边距的 space(取自其他类似主题)。

解决这两个问题的最简单方法是什么?

有了 ActionBar 你可以使用:

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayUseLogoEnabled(true);
    getSupportActionBar().setLogo(R.drawable.xxxx);

Toolbar:

    Toolbar toolbar = findViewById(R.id.toolbar);
    toolbar.setLogo(R.drawable.xxxxx);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);