简体   繁体   中英

The text inside the Toolbar is not centered

I've written the following code in XML . But the RelativeLayout does not fit into the Toolbar completely and is about 10dp from the left.

Codes of XML :

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/colorPrimary"
        >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorAccent">

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:src="@drawable/supporter"
    android:layout_alignParentRight="true"
    />

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/title_toolbar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/yekan"
    android:text="@string/appnameForUsers"
    android:textColor="@android:color/white"
    android:textSize="24sp"
    android:layout_centerHorizontal="true"
    />

<ImageView
    android:layout_width="35dp"
    android:layout_height="35dp"
    android:src="@drawable/supporter"
    android:layout_alignParentLeft="true"
    />

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

If you want to remove default padding from ToolBar

use below code in ToolBar

<android.support.v7.widget.Toolbar
  .............
android:contentInsetStart="0dp"
android:contentInsetLeft="0dp"
android:contentInsetEnd="0dp"
android:contentInsetRight="0dp"

app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
.................
/>

You need to tell to your TextView to move text to left/start and make it to use entire space. If si not desired, move it to left.

<android.support.v7.widget.AppCompatTextView
    android:id="@+id/title_toolbar"
    android:layout_width="match_parent"
    android:gravity="start"
    ....

You need to add a tag app:contentInsetStart="0dp" to remove the default padding from left side in Toolbar .

You can also add other tags like app:contentInsetEnd="0dp" if you're getting padding to the right, but in your case, it is not needed.

You'll have to add this tag to your Toolbar .

Just replace your code with below snippet of code. It'll solve your problem.

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"
            android:gravity="center">

            <ImageView
                android:id="@+id/img_1"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_alignParentRight="true"
                android:src="@drawable/supporter" />

            <android.support.v7.widget.AppCompatTextView
                android:id="@+id/title_toolbar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toStartOf="@+id/img_1"
                android:layout_toLeftOf="@+id/img_1"
                android:layout_toEndOf="@+id/img_2"
                android:layout_toRightOf="@+id/img_2"
                android:layout_centerHorizontal="true"
                android:text="@string/appnameForUsers"
                android:textColor="@android:color/white"
                android:textSize="24sp" />

            <ImageView
                android:id="@+id/img_2"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_alignParentLeft="true"
                android:src="@drawable/supporter" />

        </RelativeLayout>

    </android.support.v7.widget.Toolbar>
app:contentInsetStart="0dp"

在工具栏中添加此属性

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM