how to remove left margin of Android Toolbar?

好久不见. 提交于 2019-11-28 21:09:37
Damodhar Meshram

replace your xml with below xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/margin_padding_8dp"
    android:contentInsetStart="0dp" 
    android:contentInsetLeft="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp">

<RelativeLayout
    android:id="@+id/rlToolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingRight="@dimen/margin_padding_16dp"
        android:text="AppBar"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_20sp" />

</RelativeLayout>

Use app:contentInsetStart="0dp" to remove that left space.

See the below code an here I add app:contentInsetStart="0dp". You need to add that to your code bcoz Before version 21 i.e lollipop you need to add that line.

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:contentInsetStart="0dp"
            app:contentInsetStart="0dp"
            >
 </android.support.v7.widget.Toolbar>

Referring to @calvinfly comment:

I updated my code

<RelativeLayout
    android:id="@+id/rlTop"
    android:layout_width="fill_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/titleString"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#468bac"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/rlStarsTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:layout_marginRight="2dp"
        android:layout_toRightOf="@+id/toolbar_title"
        android:layout_toEndOf="@+id/toolbar_title"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true">

        <RatingBar
            android:id="@+id/txtRatings"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_margin="1dp"
            android:gravity="center"
            android:max="5"
            android:rating="3.7"
            android:textColor="@android:color/holo_blue_bright"
            android:textStyle="bold" />
    </RelativeLayout>

</RelativeLayout>

Add below xml code to your Toolbar !

app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"

Add following code to your .xml file It may solve. Perfectly working solution I have tried.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/white"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp"
    app:theme="@style/toolbarPopup">

above 21 use following code

<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />

This works for me...

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app2="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app2:contentInsetStart="0dp"/>

Just add these two lines in your toolbar.xml

  1. app:contentInsetStart="0dp"
  2. app:contentInsetEnd="0dp"

In toolbar you have to add app:contentInsetStart="0dp" to remove left space.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!