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

自定义Toolbar返回图标显示过大问题求助

嘿,我之前也踩过这个坑!直接用setHomeAsUpIndicator设置自定义返回图标时,系统并不会自动帮我们适配Toolbar的标准尺寸,所以才会出现图标撑得过大的情况。这里给你几个实用的解决办法:

方法1:手动设置Drawable的边界尺寸

我们可以获取Toolbar的标准高度(也就是ActionBar的默认高度),然后把自定义图标的尺寸调整到合适的比例,和系统默认图标大小对齐:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    setContentView(R.layout.profileedit); 
    mTopToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(mTopToolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    // 获取ActionBar的标准高度
    int actionBarSize = getResources().getDimensionPixelSize(R.dimen.abc_action_bar_default_height_material);
    // 加载自定义返回图标
    Drawable backIcon = ContextCompat.getDrawable(this, R.drawable.icon_back_to_home);
    // 设置图标边界,宽高取actionBarSize的80%左右,适配效果最好
    backIcon.setBounds(0, 0, (int)(actionBarSize * 0.8), (int)(actionBarSize * 0.8));
    // 设置为返回图标
    getSupportActionBar().setHomeAsUpIndicator(backIcon); 

    setTitle(""); 
}

方法2:直接通过Toolbar设置导航图标(更灵活)

放弃用ActionBar的方法,直接给Toolbar设置navigationIcon,不仅能精准控制尺寸,还能自定义点击逻辑:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    setContentView(R.layout.profileedit); 
    mTopToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(mTopToolbar); 

    // 加载图标并设置尺寸
    Drawable backIcon = ContextCompat.getDrawable(this, R.drawable.icon_back_to_home);
    // 建议在dimens.xml里定义专门的图标尺寸,比如<dimen name="toolbar_icon_size">24dp</dimen>
    int iconSize = getResources().getDimensionPixelSize(R.dimen.toolbar_icon_size);
    backIcon.setBounds(0, 0, iconSize, iconSize);
    
    mTopToolbar.setNavigationIcon(backIcon);
    // 设置返回按钮的点击事件
    mTopToolbar.setNavigationOnClickListener(v -> onBackPressed());

    setTitle(""); 
}

方法3:优化矢量图标的配置(如果是Vector Drawable)

如果你的icon_back_to_home是矢量图(.xml格式),可以检查它的尺寸配置是否符合Toolbar图标规范(通常是24dp×24dp),确保width、height和viewport参数匹配:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="24dp"
    android:height="24dp"
    android:viewportWidth="24"
    android:viewportHeight="24">
    <!-- 你的图标路径代码 -->
</vector>

问题回顾

你遇到的场景:在AppCompatActivity中使用Toolbar并启用左侧返回按钮,设置自定义返回图标后显示过大,且已排除drawable密度文件夹的问题。

布局代码

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/ll_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="0dp" 
        android:layout_height="wrap_content" 
        android:background="@color/colorPrimary" 
        android:minHeight="?attr/actionBarSize" 
        app:layout_constraintLeft_toLeftOf="parent" 
        app:layout_constraintRight_toRightOf="parent" 
        app:layout_constraintTop_toTopOf="parent" /> 

    <ScrollView /> 

</android.support.constraint.ConstraintLayout>

Activity的onCreate代码

@Override 
public void onCreate(Bundle savedInstanceState) { 
    setContentView(R.layout.profileedit); 
    mTopToolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(mTopToolbar); 
    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    getSupportActionBar().setHomeAsUpIndicator(ContextCompat.getDrawable(this, R.drawable.icon_back_to_home)); 
    setTitle(""); 
}

问题截图

自定义返回图标过大

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 09:26:05