RelativeLayout与ToolBar内的父级宽度不匹配 [英] RelativeLayout is not matching parent width inside a ToolBar

查看:113
本文介绍了RelativeLayout与ToolBar内的父级宽度不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

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

如您所见,RelativeLayout周围的蓝色框不在屏幕边缘,也不会让我将其带到边缘.

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.

RelativeLayout或它的父级都没有任何填充或边距,因此我很困惑为什么这是一个问题以及如何解决它.

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.

推荐答案

您需要在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>

这是开发者文档中的内容

这篇关于RelativeLayout与ToolBar内的父级宽度不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆