When you add a custom view to the Android toolbar, marginLeft will appear

I have some problems with the Android toolbar. Usually, if I set a custom view on a toolbar, the view should fill the entire space of the toolbar from left to right and has no margin.

but I have an empty space on the left, this is my code:

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="46dip"
        android:background="?attr/colorPrimary" />

    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

activity:

private void initToolbar() {
    toolbar = (Toolbar) findViewById(R.id.base_toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if( actionBar != null)
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    contentLayout = (FrameLayout) findViewById(R.id.base_content);
}

Is something wrong with my code, or should it be like that?

+4
source share
2 answers

The space on the left is caused by the contents of the InsetStart toolbar, which is 16dp by default. You can set it to 0 to remove it. Do not forget to add the circuit.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/base_toolbar"
        android:layout_width="match_parent"
        android:layout_height="46dip"
        android:background="?attr/colorPrimary"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp" />

    <FrameLayout
        android:id="@+id/base_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
+4
source

,

<android.support.v7.widget.Toolbar
        android:id="@+id/my_awesome_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1E90FF"
        android:minHeight="?attr/actionBarSize" >

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="Toolbar Title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff" />
    </android.support.v7.widget.Toolbar>
0

All Articles