簡體   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