Chrome自定义标签会更改默认关闭按钮不起作用

时间:2017-03-11 19:54:15

标签: android chrome-custom-tabs

我正在尝试更改自定义Chrome标签的操作栏上的默认关闭按钮。我尝试使用setCloseButtonIcon()进行设置但是,默认关闭按钮仍会显示。我想把近处更改为箭头。

我的代码如下:

public void openHomePage() {
    final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder();
    builder.setToolbarColor(ContextCompat.getColor(getActivity(), R.color.primary));
    final Bitmap backButton = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_black_48dp);
    builder.setCloseButtonIcon(backButton);

    builder.setShowTitle(true);
    final CustomTabsIntent customTabsIntent = builder.build();

    customTabsIntent.launchUrl(getActivity(), Uri.parse(mTvHomepage.getText().toString()));
}

7 个答案:

答案 0 :(得分:19)

我有一个观察。上个月,在SO搜索各种Chrome自定义标签问题时,我发现此answer建议使用24dp大小的图标,并且还发现此question说它可以正常使用PNG。

我已使用here.

中的后退箭头图标检查了您的代码

当我使用“ic_arrow_back_black_48dp”时,它没有将默认关闭按钮更改为箭头(参见左图)。

final Bitmap backButton = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_black_48dp);

但是当我使用“ic_arrow_back_black_24dp”时,它完全将默认关闭按钮更改为箭头(参见右图)。

final Bitmap backButton = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_black_24dp);

由于它对我来说非常有用,您还应该尝试使用here中的“24dp”尺寸后退箭头图标,而不是“48dp”尺寸箭头图标。

屏幕截图:[设备:ASUS_Z00UD;操作系统:6.0.1]

The default close button has changed to an arrow when using 24dp size icon instead of 48dp size.

答案 1 :(得分:8)

假设您使用的是Google库,而不是相关的库,则图标大小应为24dp documented here

这可以使用BitmapFactory选项实现:

BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth = 24;
options.outHeight = 24;
options.inScaled = true; //already default, just for illustration - ie scale to screen density (dp)
... = BitmapFactory.decodeResource(getResources(), R.drawable.ic_arrow_back_black_48dp, opts);

答案 2 :(得分:4)

您可以直接从BitmapDrawable获取Drawable,但不能从VectorDrawable直接获取,setCloseButtonIcon需要@NonNull Bitmap icon

您也可以使用 svg ,如下所示。从这里下载svg ic_arrow_back_black_24px

以下方法是不言自明的:

private static Bitmap getBitmapFromDrawable(Context context, int drawableId) {
Drawable drawable = ContextCompat.getDrawable(context, drawableId);
if (drawable instanceof BitmapDrawable) {
  return ((BitmapDrawable) drawable).getBitmap();
} else if (drawable instanceof VectorDrawable) {
  return getBitmapFromVectorDrawable((VectorDrawable) drawable);
} else {
  throw new IllegalArgumentException("Unable to convert to bitmap");
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static Bitmap getBitmapFromVectorDrawable(VectorDrawable vectorDrawable) {
Bitmap bitmap = Bitmap.createBitmap(vectorDrawable.getIntrinsicWidth(),
    vectorDrawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
vectorDrawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
vectorDrawable.draw(canvas);
return bitmap;
}

您可以将以上内容用作:

builder.setCloseButtonIcon(getBitmapFromDrawable(this, R.drawable.ic_arrow_back_black_24px)); 

Ref from SO

答案 3 :(得分:2)

为什么不添加图像资产并存储在mipmap中然后在android studio中使用内置的默认图标会更容易 RSCh

上传后您可以使用ImageView中的src资源轻松地从xml文件中的mipmap访问图标和图像

var url = '/anysource/anypath/myfilename.gif';
var filename = url.slice(url.lastIndexOf('/')+1,url.length);

答案 4 :(得分:1)

要使用任何 24dp可绘制资源在Kotlin(使用Android KTX)中进行这项工作,

AppCompatResources.getDrawable(activity, R.drawable.ic_arrow_back_white_24dp)?.let {
    builder.setCloseButtonIcon(it.toBitmap())
}

如果您需要进行一些着色:

AppCompatResources.getDrawable(activity, R.drawable.ic_arrow_back_black_24dp)?.mutate()?.let {
    DrawableCompat.setTint(it, Color.WHITE)
    builder.setCloseButtonIcon(it.toBitmap())
}

如果需要调整可绘制对象的大小,则将新的宽度/高度传递给Drawable.toBitmap()函数。

如果您不使用Kotlin,则可以使用与Drawable.toBitmap()代码等效的代码:

fun Drawable.toBitmap(
    @Px width: Int = intrinsicWidth,
    @Px height: Int = intrinsicHeight,
    config: Config? = null
): Bitmap {
    if (this is BitmapDrawable) {
        if (config == null || bitmap.config == config) {
            // Fast-path to return original. Bitmap.createScaledBitmap will do this check, but it
            // involves allocation and two jumps into native code so we perform the check ourselves.
            if (width == intrinsicWidth && height == intrinsicHeight) {
                return bitmap
            }
            return Bitmap.createScaledBitmap(bitmap, width, height, true)
        }
    }

    val (oldLeft, oldTop, oldRight, oldBottom) = bounds

    val bitmap = Bitmap.createBitmap(width, height, config ?: Config.ARGB_8888)
    setBounds(0, 0, width, height)
    draw(Canvas(bitmap))

    setBounds(oldLeft, oldTop, oldRight, oldBottom)
    return bitmap
}

有关更多信息,请参见this答案。

答案 5 :(得分:1)

我也不得不面对同样的问题

解决方案:-

1)采用image(back arrow)格式的png
2)将图片尺寸保持在24dp下。

答案 6 :(得分:0)

我必须去以下站点:https://material.io/tools/icons/?icon=keyboard_backspace&style=baseline

我得到了PNG,然后以这种方式使用了。我知道我参加晚会很晚,但这是给任何需要的人的。