Android Chrome自定义选项卡更改默认关闭按钮不工作
我正在尝试更改自定义chrome选项卡操作栏上的默认关闭按钮。我尝试使用Android Chrome自定义选项卡更改默认关闭按钮不工作,android,chrome-custom-tabs,Android,Chrome Custom Tabs,我正在尝试更改自定义chrome选项卡操作栏上的默认关闭按钮。我尝试使用setCloseButtonIcon()进行设置,但是默认的关闭按钮仍然显示。我想把它改成接近箭头的形状 我的代码如下: public void openHomePage() { final CustomTabsIntent.Builder builder = new CustomTabsIntent.Builder(); builder.setToolbarColor(ContextCompat.getCo
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()));
}
假定您使用的是库,而不是相关的库,则图标大小应为24dp 这可以通过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);
我有一个观察。上个月,在搜索各种chrome自定义标签问题时,我发现这建议使用24dp大小的图标,还发现这表示它可以与PNG配合使用 我已使用中的返回箭头图标检查了您的代码 当我使用“ic_arrow_back_black_48dp”时,它没有将默认的关闭按钮更改为箭头(参见左图) 但当我使用“ic_arrow_back_black_24dp”时,它完美地将默认的关闭按钮更改为箭头(参见右图) 由于它对我非常有效,您还应该尝试使用中的“24dp”大小后向箭头图标,而不是“48dp”大小后向箭头图标 截图:[设备:华硕;操作系统:6.0.1]
您可以直接从
可绘制
获取位图可绘制
,但不能从矢量可绘制
获取,因为设置关闭按钮图标
需要@非空位图图标
您还可以使用svg,如下所示。从这里下载svg
以下方法不言自明:
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));
为什么不添加图像资产并存储在mipmap中,这样使用安卓studio内置的默认图标就更容易了 上传后,您可以使用ImageView中的src资源轻松访问xml文件中mipmap中的图标和图像
android:src="@mipmap/ic_launcher"
我必须去这个网站:
我得到了PNG,然后用那种方式。我知道我参加聚会迟到了,但这是为任何需要的人准备的。要在Kotlin(使用)任何24 DP可提取资源来完成这项工作:
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()
code:
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
}
更多信息请参见答案。我也不得不面对同样的问题 解决方案:- 1) 以
png
格式拍摄图像(后箭头)
。2) 将图像大小保持在
24dp
以下我刚刚检查了我的代码,它看起来完全一样。你有没有试过另一个可拖动的(可能是更小的?48dp箭头太大可能是你的问题吗?@stamanuel首先我试了24dp,但没用。经过一些搜索,有人说使用48dp。然而,48dp也不起作用。你能发布一个基本的项目吗?@FantasyPollock似乎你只能使用png图标。我最初尝试使用svg图标。当我改成png时,它开始工作了。不确定svg为什么不工作。我使用了与您相同的图标,但选择了png。您是否使用svg图标进行了测试?谢谢。@ant2009很抱歉,我忘了提到.png格式。是的,我总是用png图标测试。不确定svg图标为什么不工作。+1我支持大家在这里所说的。如果您想更改“关闭图标”,它必须是24dp PNG可绘制的。向量或任何其他大小都不起作用。。。
AppCompatResources.getDrawable(activity, R.drawable.ic_arrow_back_black_24dp)?.mutate()?.let {
DrawableCompat.setTint(it, Color.WHITE)
builder.setCloseButtonIcon(it.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
}