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

如何设置Android Studio中Toolbar下方的背景图片?

解决Toolbar覆盖背景图的问题

要把背景图放到Toolbar下方,核心就是调整布局的层级顺序——Android里布局控件的显示层级是代码写在后面的,显示在更上层。所以只需要把背景图的布局代码放在<include toolbar>的前面就行。

具体操作示例

假设你原来的布局是先写Toolbar再写背景图:

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

<!-- 你的背景图控件,比如ImageView -->
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/your_background"
    android:scaleType="centerCrop" />

现在把背景图移到Toolbar前面:

<!-- 背景图放在前面,层级更低,会被后面的Toolbar覆盖 -->
<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/your_background"
    android:scaleType="centerCrop" />

<include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

额外注意点

  • 如果你的Toolbar是完全不透明的,就算背景图在下面也看不到,这时候可以修改toolbar.xml里的背景属性,比如设置成透明或半透明:
    <!-- 在toolbar.xml中添加 -->
    android:background="@android:color/transparent"
    <!-- 或者半透明颜色,比如半透黑 -->
    android:background="#80000000"
    
  • 如果用的是ConstraintLayout/RelativeLayout,只要保证背景图的布局代码在Toolbar之前,就能实现层级关系,不需要额外的对齐属性(当然Toolbar还是要放在顶部位置)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 16:30:56