在Android工具栏图标中删除边距 [英] Remove margin in android toolbar icon

查看:94
本文介绍了在Android工具栏图标中删除边距的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用工具栏中,我在工具栏的图标和导航图标之间得到了这个怪异的空白(如图所示). 我不知道它来自哪里以及如何删除它.搜索互联网后,我发现了这一点:

I get this weird margin in my app toolbar between icon and navigation icon in the toolbar (as in the image). I've got no idea about where it comes from and how to remove it. After searching the internet I found this:

<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>

但是我仍然可以得到这个利润,如图所示:

But I still get this margin as in the figure:

编辑>>解决方案

在使用布局绑定后,我发现很多边距都是图标的(如图所示).但是我仍然可以删除此边距并更改图标和标题文本的大小吗?

Well after using layout bound I figured much of the margins are of the icon(as in figure). But can I still remove this margin and change the size of the icon and the title text.

修改

以下@Amir解决方案: Java助手:

Following @Amir solution: Helper for 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);
    }
}

}

在您的活动课中:

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

推荐答案

您可以轻松删除保证金|在标题和后退图标之间填充:

app:contentInsetStartWithNavigation="0dp"

保证金|填充在工具栏的左侧/右侧,其中:

Margin | padding In left/right side of toolbar with:

app:contentInsetStart="0dp"

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

Also if you need more customization do with following:

<?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工具栏图标中删除边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆