RelativeLayout与ToolBar中的父宽度不匹配

问题描述 投票:1回答:1

所以我的主屏幕上有一个自定义的工具栏,在这个工具栏中是一个RelativeLayout。我将layout width设置为match_parent,相对布局的父级只是屏幕的宽度。

由于某种原因,它不适合屏幕的宽度,因为左边缘距离屏幕边缘有一段距离。我不确定为什么会这样,但它使RelativeLayout内的定位更加困难。

这是我的.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>

这是一个显示我正在谈论的截图。

enter image description here

正如你所看到的,RelativeLayout周围的蓝色框不在屏幕的边缘,也不会让我把它带到边缘。

RelativeLayout或其父母都没有任何填充或边距,所以我很困惑为什么这是一个问题以及我如何解决它。

android android-toolbar android-relativelayout
1个回答
1
投票

您需要在ToolBar中设置以下属性

app:contentInsetLeft="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/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>

这是来自developer's documentation

© www.soinside.com 2019 - 2024. All rights reserved.