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

时间:2016-07-11 07:13:13

标签: android android-toolbar margins

我的app工具栏中的图标和导航图标之间的这个奇怪边距(如图中所示)。 我不知道它来自何处以及如何将其删除。在搜索互联网后,我发现了这个:

<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

修改&gt;&gt;解决方案

使用布局绑定后,我认为很多边距都是图标(如图所示)。但是我仍然可以删除此边距并更改图标和标题文本的大小。 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);
    }
}

}

在您的活动课程中:

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

5 个答案:

答案 0 :(得分:40)

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

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>

答案 1 :(得分:1)

您可以通过修改主题和样式来调整边距,如下所示:

<style name="cusToolbarNavigationButtonStyle" parent="@style/Widget.AppCompat.Toolbar.Button.Navigation">
    <!--default is 56dp-->
    <item name="android:minWidth">0dp</item>  
    <item name="android:paddingLeft">16dp</item>
    <item name="android:paddingRight">16dp</item>
</style>

<style name="cusToolbarStyle" parent="@style/Widget.AppCompat.Toolbar">
    <!--default 4dp-->
    <item name="titleMargin">0dp</item> 
    <!--default @dimen/abc_action_bar_content_inset_with_nav-->
    <item name="contentInsetStartWithNavigation">0dp</item> 
</style>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="toolbarNavigationButtonStyle">@style/cusToolbarNavigationButtonStyle</item>
    <item name="toolbarStyle">@style/cusToolbarStyle</item>

</style>

答案 2 :(得分:0)

如果要从CollapsingToolbarLayout中删除标题中的边距/填充,您可能会发现这个有用:

<android.support.design.widget.CollapsingToolbarLayout
    app:expandedTitleMarginStart="0dp"
    .../>

答案 3 :(得分:0)

如果您正在使用此

   <android.widget.Toolbar>
    android:contentInsetStart="0dp"
    android:contentInsetLeft="0dp"
</android.widget.Toolbar>

如果使用

    <androidx.appcompat.widget.Toolbar>

    app:contentInsetStart="0dp"
    app:contentInsetLeft="0dp"
</androidx.appcompat.widget.Toolbar>

答案 4 :(得分:0)

private readonly Random randomColor = new Random();

public Form1()
{
    InitializeComponent();
    tmrStep.Tick += tmrStep_Tick;
    tmrStep.Start();
}

private void tmrStep_Tick(object sender, EventArgs e)
{
    Invalidate();
}

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);

    Graphics g = e.Graphics;

    Color defaultColor = Color.FromArgb(randomColor.Next(255), randomColor.Next(255), randomColor.Next(255));
    using (Pen defaultPen = new Pen(defaultColor, 2))
    {
        int intHeight = this.ClientSize.Height;
        int intWidth = this.ClientSize.Width;

        int intMiddleHeight = intHeight / 2;
        int intMiddleWidth = intWidth / 2;

        Point posTopLeft = new Point(0, 0);
        Point posTopRight = new Point(intWidth, 0);
        Point posBottomLeft = new Point(0, intHeight);
        Point posBottomRight = new Point(intWidth, intHeight);
        Point posMiddle = new Point(intMiddleWidth, intMiddleHeight);
        Point posMiddleTop = new Point(intMiddleWidth, 0);
        Point posMiddleLeft = new Point(0, intMiddleHeight);
        Point posMiddleRight = new Point(intWidth, intMiddleHeight);
        Point posMiddleBottom = new Point(intMiddleWidth, intHeight);

        int defaultStep = randomColor.Next(3, 10);

        int b1;
        for (b1 = 0; b1 <= intMiddleWidth; b1 += defaultStep)
        {
            g.DrawLine(defaultPen, posTopLeft.X + b1, posTopLeft.Y, posMiddle.X - b1, posMiddle.Y);
            g.DrawLine(defaultPen, posTopRight.X - b1, posTopRight.Y, posMiddle.X + b1, posMiddle.Y);
            g.DrawLine(defaultPen, posMiddle.X - b1, posMiddle.Y, posBottomLeft.X + b1, posBottomLeft.Y);
            g.DrawLine(defaultPen, posMiddle.X + b1, posMiddle.Y, posBottomRight.X - b1, posBottomRight.Y);
        }

        int b2;
        for (b2 = 0; b2 <= intMiddleHeight; b2 += defaultStep)
        {
            g.DrawLine(defaultPen, posMiddleTop.X, posMiddleTop.Y + b2, posMiddleLeft.X, posMiddleLeft.Y - b2);
            g.DrawLine(defaultPen, posMiddleTop.X, posMiddleTop.Y + b2, posMiddleRight.X, posMiddleRight.Y - b2);
            g.DrawLine(defaultPen, posMiddleRight.X, posMiddleRight.Y + b2, posMiddleBottom.X, posMiddleBottom.Y - b2);
            g.DrawLine(defaultPen, posMiddleLeft.X, posMiddleLeft.Y + b2, posMiddleBottom.X, posMiddleBottom.Y - b2);
        }
    }
}