You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何增大Toolbar中Material Font.Image图标的尺寸?

调整Toolbar上FontImage图标的尺寸方案

针对平板及桌面端需求,以下几种方案可实现Toolbar返回箭头、勾选标记等FontImage图标的尺寸调整:

方案一:通过Toolbar样式全局设置

直接在Toolbar的自定义样式中指定android:iconSize属性,该属性会统一控制导航图标和菜单动作图标的大小:

<!-- styles.xml -->
<style name="TabletToolbarStyle" parent="Widget.MaterialComponents.Toolbar.Primary">
    <!-- 根据平板/桌面需求设置尺寸,示例为36dp -->
    <item name="android:iconSize">36dp</item>
    <!-- 可选:指定导航图标 -->
    <item name="navigationIcon">@drawable/ic_nav_back</item>
</style>

在布局中应用该样式:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/main_toolbar"
    style="@style/TabletToolbarStyle"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize" />

方案二:代码动态修改FontDrawable尺寸

如果需要针对单个图标调整,可直接获取对应的FontDrawable实例,修改其固有尺寸:

Kotlin 示例

// 扩展函数:dp转px
val Int.dp: Int
    get() = (this * Resources.getSystem().displayMetrics.density).toInt()

// 调整返回箭头尺寸
val navIcon = toolbar.navigationIcon as? FontDrawable
navIcon?.apply {
    setIntrinsicSize(36.dp, 36.dp)
    toolbar.navigationIcon = this
}

// 调整菜单中的勾选标记尺寸
toolbar.setOnMenuItemClickListener { item ->
    if (item.itemId == R.id.action_done) {
        val doneIcon = item.icon as? FontDrawable
        doneIcon?.apply {
            setIntrinsicSize(36.dp, 36.dp)
            item.icon = this
        }
        true
    } else {
        false
    }
}

Java 示例

// dp转px工具方法
public static int dpToPx(int dp) {
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

// 调整返回箭头尺寸
Drawable navIcon = toolbar.getNavigationIcon();
if (navIcon instanceof FontDrawable) {
    FontDrawable fontNavIcon = (FontDrawable) navIcon;
    fontNavIcon.setIntrinsicSize(dpToPx(36), dpToPx(36));
    toolbar.setNavigationIcon(fontNavIcon);
}

// 调整菜单勾选标记尺寸
toolbar.setOnMenuItemClickListener(item -> {
    if (item.getItemId() == R.id.action_done) {
        Drawable doneIcon = item.getIcon();
        if (doneIcon instanceof FontDrawable) {
            FontDrawable fontDoneIcon = (FontDrawable) doneIcon;
            fontDoneIcon.setIntrinsicSize(dpToPx(36), dpToPx(36));
            item.setIcon(fontDoneIcon);
        }
        return true;
    }
    return false;
});

方案三:自定义FontDrawable子类全局预设尺寸

如果所有FontImage都需要统一大尺寸,可自定义FontDrawable子类,在构造时指定固有尺寸:

class LargeFontDrawable(context: Context, resId: Int) : FontDrawable(context, resId) {
    init {
        val size = 36.dp
        setIntrinsicSize(size, size)
    }

    private val Int.dp: Int
        get() = (this * Resources.getSystem().displayMetrics.density).toInt()
}

使用时直接实例化该类作为图标:

toolbar.navigationIcon = LargeFontDrawable(this, R.drawable.ic_nav_back)

方案四:自定义Toolbar布局完全控制图标

若需要更灵活的布局控制,可直接在Toolbar内部添加ImageView,手动设置尺寸和点击事件:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <!-- 返回箭头图标 -->
    <ImageView
        android:id="@+id/nav_back_btn"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/ic_nav_back"
        android:contentDescription="@string/nav_back"
        android:clickable="true" />

    <!-- 标题文本 -->
    <TextView
        android:id="@+id/toolbar_title"
        style="@style/TextAppearance.MaterialComponents.Headline6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <!-- 勾选标记图标 -->
    <ImageView
        android:id="@+id/done_btn"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:src="@drawable/ic_done"
        android:contentDescription="@string/done"
        android:clickable="true" />

</com.google.android.material.appbar.MaterialToolbar>

在代码中设置点击逻辑:

nav_back_btn.setOnClickListener { onBackPressedDispatcher.onBackPressed() }
done_btn.setOnClickListener { /* 处理完成逻辑 */ }

内容的提问来源于stack exchange,提问作者curtjacobs1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 09:42:44