Android中Toolbar嵌套的LinearLayout左侧出现多余空白是什么原因?
问题根因
Toolbar 默认设置了左侧内容插入边距,用于预留导航图标位置,该边距不属于内部子控件的 padding 或 margin,因此修改子布局的相关参数无法消除空白。
解决方案
在你的 Toolbar 标签中添加以下两个属性,清除默认左侧边距即可:
app:contentInsetLeft="0dp" app:contentInsetStart="0dp"
如果需要同步清除右侧默认边距,可额外添加:
app:contentInsetRight="0dp" app:contentInsetEnd="0dp"
问题相关截图

修改后完整布局代码
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="wrap_content"> <androidx.appcompat.widget.Toolbar android:id="@+id/myToolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentInsetLeft="0dp" app:contentInsetStart="0dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#019032" android:padding="20dp" android:gravity="center_horizontal"> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:layout_marginLeft="0dp" android:paddingLeft="0dp" android:src="@drawable/menu" /> <TextView android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="center" android:textAlignment="center" android:layout_weight="1" android:textStyle="bold" android:textSize="18dp" android:text="Falesie in Italia" /> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@drawable/menu" /> </LinearLayout> </androidx.appcompat.widget.Toolbar> </androidx.constraintlayout.widget.ConstraintLayout>
内容的提问来源于stack exchange,提问作者imaluris
相关产品推荐
相关产品推荐

