android - 删除 android 工具栏图标中的边距

标签 android android-toolbar margins

我的应用工具栏中的图标和工具栏中的导航图标之间出现了这个奇怪的边距(如图所示)。 我不知道它来自哪里以及如何删除它。在网上搜索后,我发现了这个:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:background="?attr/colorPrimaryDark"
    android:layout_margin="0dp"
    android:contentInsetLeft="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetStart="0dp"
    android:contentInsetEnd="0dp"
    android:padding="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetEnd="0dp">
</android.support.v7.widget.Toolbar>

但我仍然得到这个边距,如图所示: Margin

编辑>>解决方案

在使用布局绑定(bind)之后,我发现大部分边距都是图标的(如图)。但是我仍然可以删除此边距并更改图标和标题文本的大小吗? enter image description here

编辑

遵循@Amir 解决方案: java的助手:

class BasicActivity extends AppCompatActivity{
    protected Toolbar mToolbar;    /// Initilize it in onCreate methode
    .....

     protected void setupToolbar(String title) {
        toolbar=(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar ab = getSupportActionBar();
        if (ab != null) {
            ab.setDisplayHomeAsUpEnabled(true);
            ab.setDisplayShowHomeEnabled(true);
        }

       if (!TextUtils.isEmpty(title)) {
        setTitle(title);
    }
}

}

在你的 Activity 课上:

class Main extends BasicActivity{
     @override
     protected void onCreate(Bundle saved){
          super.onCreate(saved);
          ....
          setupToolbar("MAIN");
     }
}

最佳答案

您可以轻松删除 Margin |标题和后退图标之间的填充:

app:contentInsetStartWithNavigation="0dp"

边距 |填充在工具栏的左侧/右侧:

app:contentInsetStart="0dp"

此外,如果您需要更多自定义,请执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/color_primary"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/icon_toolbar_left"
            style="@style/IconFont.Large"
            android:layout_width="48dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="?attr/selectableItemBackground" />



        <TextView
            android:id="@+id/text_toolbar_title"
            style="@style/Textview.White.MediumSmall"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/icon_toolbar_right"
            android:layout_toRightOf="@+id/icon_toolbar_left"
            android:gravity="center"
            android:text="@string/nav_category"/>


        <ImageView
            android:id="@+id/icon_toolbar_right"
            style="@style/IconFont.Large"
            android:layout_width="48dp"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="?attr/selectableItemBackground"/>

    </RelativeLayout>

</android.support.v7.widget.Toolbar>

关于android - 删除 android 工具栏图标中的边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38301115/

相关文章:

android - 如何在 android 布局上实现左/右滑动/滑动

java - 保留 ListView 中项目的检查状态

android - 如何确定手机是否有缺口

python - Python 中的 Windows 7 MARGINS 结构

c++ - Qt 去除边距

Android 2.3 渐进式下载不工作

java - 从另一个 apk 加载资源(布局)

android - 添加 View 时工具栏标题消失

android - 在 Android 中以编程方式隐藏工具栏主页/后退按钮?

css - 在 CSS 中,让 Logo 位于中央布局之上并向左突出?