Chrome Custom Tabs change the default close button not working

前端 未结 7 580
清歌不尽
清歌不尽 2021-01-17 11:30

I am trying to change the default close button on the actionbar of the custom chrome tabs. I have tried to set using setCloseButtonIcon() However, the default c

7条回答
  •  花落未央
    2021-01-17 11:53

    You can directly get BitmapDrawable from Drawable but not from VectorDrawable as setCloseButtonIcon requires @NonNull Bitmap icon

    You can also use svg as follows. Download the svg from here ic_arrow_back_black_24px

    Below methods are self explanatory:

    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;
    }
    

    You can use the above as:

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

    Ref from SO

提交回复
热议问题