Android ConstraintLayout中引入的Toolbar存在异常左侧边距问题
Hey there! Let's figure out why your included new_toolbar is showing that annoying left gap next to the parent Toolbar. I’ve run into this exact issue a few times, so here are the most likely fixes to try:
1. Fix the <include> tag constraints in your Activity.xml
When using ConstraintLayout, the <include> tag itself needs proper constraints to align perfectly with the parent container. If it’s not pinned to the left edge, it’ll default to some unintended margin. Update your include tag to look like this:
<include layout="@layout/new_toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_marginStart="0dp" android:layout_marginLeft="0dp"/>
Make sure you remove any extra layout_marginStart/layout_marginLeft values that might be adding space, and confirm layout_width is set to match_parent.
2. Override Toolbar's default content insets in new_toolbar.xml
This is the most common culprit! The Android Toolbar widget has a default contentInsetStart (usually 16dp) that pushes content to the right, even if you set padding/margin to 0. You need to explicitly disable this:
If your new_toolbar.xml uses Toolbar as the root element:
<Toolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/new_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" <!-- These two lines fix the default left inset --> app:contentInsetStart="0dp" app:contentInsetStartWithNavigation="0dp"/>
If your new_toolbar.xml uses ConstraintLayout as the root:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:padding="0dp" android:layout_margin="0dp"> <Toolbar android:id="@+id/new_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/colorPrimary" app:contentInsetStart="0dp" app:contentInsetStartWithNavigation="0dp" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
The contentInsetStartWithNavigation line is crucial if you’re using a back button or navigation icon—it ensures that element also aligns to the left edge without extra space.
3. Check for theme-related padding
If the above fixes don’t work, your app theme might be applying default padding to Toolbars. To override this, add these attributes to your new_toolbar.xml Toolbar:
android:paddingStart="0dp" android:paddingLeft="0dp"
Start with the first two fixes—they resolve 90% of this left gap issue with included Toolbars in ConstraintLayout. Let me know if you still run into problems!
内容的提问来源于stack exchange,提问作者danmaslo

