繁体   English   中英

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

[英]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));中使用的空间。 对于 mocking 的边距(取自其他类似主题)。

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

使用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);

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM