Extra padding at the left end of action bar (after

2019-08-07 17:42发布

问题:

I don't know how this padding came up, but there's no such padding until I updated my SDK to 21. Here's my code:

    mActionBar = getSupportActionBar();
    mActionBar.setDisplayShowHomeEnabled(false);
    mActionBar.setDisplayShowTitleEnabled(false);
    actionBarView = LayoutInflater.from(this).inflate(R.layout.title_bar_layout, null);

    mActionBar.setCustomView(actionBarView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    mActionBar.setDisplayShowCustomEnabled(true);

Does anyone know how to solve this ? thanks.

回答1:

EDIT:

since the last update of Support library 22.1.x, if you are not using ToolBar the attributes have to be part of the ActionBar style. E.g

<style name="MyStyle" parent="Widget.AppCompat.Light.ActionBar.Solid.Inverse">
    <item name="contentInsetStart">0dp</item>
    <item name="contentInsetEnd">0dp</item>

    <item tools:ignore="NewApi" name="android:contentInsetStart">0dp</item>
    <item tools:ignore="NewApi" name="android:contentInsetEnd">0dp</item>
</style>

and referenced by <item name="android:actionBarStyle"

OLD ANSWER

to remove the extra padding add

 <style name="AppThemeToolbar" parent="Widget.AppCompat.Toolbar">
        <item name="contentInsetStart">0dp</item>
        <item tools:ignore="NewApi" name="android:contentInsetStart">0dp</item>
 </style>

to your style.xml file. And reference it from your app theme with:

<item name="android:toolbarStyle" tools:ignore="NewApi">@style/AppThemeToolbar</item>
<item name="toolbarStyle">@style/AppThemeToolbar</item>