如何从工具栏中的标题中删除填充?

时间:2016-07-24 16:16:21

标签: android android-toolbar

我的工具栏

My toolbar

Google Play工具栏

GooglePlay toolbar

如何删除不必要的填充?

我的工具栏在片段内 片段中的代码:

public void setUpToolbar(Toolbar toolbar, String title, @DrawableRes int resId) {
    toolbar.setTitle(title);
    ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
    ActionBar ab = ((AppCompatActivity) getActivity()).getSupportActionBar();
    ab.setHomeAsUpIndicator(resId);
    ab.setDisplayHomeAsUpEnabled(true);
}

xml中的工具栏:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

5 个答案:

答案 0 :(得分:23)

在工具栏中使用xml标记

app:contentInsetStartWithNavigation="0dp"

答案 1 :(得分:13)

只需添加工具栏

即可
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"

应该有效

答案 2 :(得分:2)

在您的代码中尝试此操作:

ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
View customView = getLayoutInflater().inflate(R.layout.main_action_bar, null);
actionBar.setCustomView(customView);
Toolbar parent =(Toolbar) customView.getParent();
parent.setContentInsetsAbsolute(0,0);

答案 3 :(得分:1)

尝试添加app:contentInsetLeft:

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

答案 4 :(得分:0)

最近遇到了这个问题,即使将所有 insets 设置为 0dp 后,NavigationIcon 和 Title 之间仍然存在差距。显然这是 TitleView 的边距,要删除它,我们必须设置 app:titleMarginStart="0dp"

所以总的来说,必须执行以下操作才能消除整个间隙。

app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp"
app:titleMarginStart="0dp"
相关问题