RelativeLayout is not matching parent width inside a ToolBar

感情迁移 提交于 2019-12-24 03:15:08

问题


So I have a customized toolbar on my home screen, and within this toolbar is a RelativeLayout. I have the layout width set to match_parent and the parent of the relative layout is just the width of the screen.

For some reason, it does not fit the width of the screen because the left edge is some distance away from the edge of the screen. I am not sure why this is but it is making positioning within the RelativeLayout a bit more difficult.

This is my .xml

<android.support.v7.widget.Toolbar
    android:id="@+id/products_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@drawable/bg_gradient"
    app:layout_constraintStart_toStartOf="parent"
    app:titleTextColor="#000000">

    <RelativeLayout
        android:id="@+id/topRL"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="16dp">

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="72sp"
            android:layout_height="37sp"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:layout_marginStart="137dp"
            android:layout_marginTop="11dp"
            android:layout_marginEnd="159dp"
            android:fontFamily="@font/veganstyle"
            android:gravity="center"
            android:text="BL"
            android:textAlignment="center"
            android:textColor="#efefef"
            android:textSize="20dp"
            android:textStyle="bold" />

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

And this is a screenshot showing what I am talking about.

As you can see, the blue box surrounding the RelativeLayout is not at the edge of the screen, nor will it let me bring it to the edge.

Neither the RelativeLayout or its parent have any padding or margins, so I am confused why this is an issue and how I can solve it.


回答1:


You need to set the following attribute in your ToolBar

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

Hence your layout should be something like the 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/products_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:titleTextColor="#000000">

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

        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="72sp"
            android:layout_height="37sp"
            android:layout_centerInParent="true"
            android:text="BL"
            android:textAlignment="center"
            android:textColor="#efefef"
            android:textSize="20dp"
            android:textStyle="bold" />

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

Here is this from the developer's documentation.



来源:https://stackoverflow.com/questions/54797519/relativelayout-is-not-matching-parent-width-inside-a-toolbar

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