简体   繁体   English

从工具栏删除默认边距底部

[英]remove default margin bottom from Toolbar

In my application I use the android.support.v7.widget.Toolbar for the toolbar. 在我的应用程序中,我将android.support.v7.widget.Toolbar用于工具栏。

Here is my layout 这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.todo.app.MainActivity"
style="@style/MainActivityBg">

<android.support.design.widget.AppBarLayout
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/AppTheme.AppBarOverlay"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        style="@style/AppTheme.ActionBar"/>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main"/>

and

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/activity_main"
tools:context="com.todo.app.MainActivity"
android:layout_marginTop="5dp">  <!-- for adding some space between the toolbar and the rest, and this is the cause of the problem! -->

<android.support.v7.widget.RecyclerView
android:id="@+id/task_list"
android:layout_width="match_parent"
android:layout_height="match_parent" />

I try to add some space between the toolbar and the rest of the view. 我尝试在工具栏和视图的其余部分之间添加一些空间。 To do that I added 5dp in marginTop to the layout which is right under the view. 为此,我在marginTop中将5dp添加到了视图下方的布局中。 And this causes this problem: While I scrolling the view there is some space remaining under the toolbar. 这就导致了这个问题:在滚动视图时,工具栏下方还有一些空间。

滚动之前

开始滚动后

In my style files xml, there is no style for margin or padding. 在我的样式文件xml中,没有用于边距或填充的样式。 How I can adding some space between toolbar and the rest without trigger this problem (when I scrolling the view)? 如何在工具栏和其余工具之间添加一些空间而又不会触发此问题(滚动视图时)?

Reason 原因

Based on the xml files, it seems to me that: 基于xml文件,在我看来:

<android.support.constraint.ConstraintLayout
    ...
    android:layout_marginTop="5dp"> 

is the reason for cropping RecyclerView content. 是裁剪RecyclerView内容的原因。

When you set the layout_marginTop between the AppBarLayout and the upper edge of ConstraintLayout appears a 5dp high empty space. 当您设置layout_marginTop之间AppBarLayout和上部边缘ConstraintLayout出现5dp高空白。

You can confirm that by turning on one of the Android options for developers: Show Layout Bounds . 您可以通过为开发人员打开Android选项之一来确认这一点: Show Layout Bounds

Solution

For the effect which you want to achieve try to remove android:layout_marginTop and set android:paddingTop for the RecyclerView but also set android:clipToPadding to false . 对于您想要实现的效果,请尝试删除android:layout_marginTop并将RecyclerView设置为android:paddingTop ,但还要将android:clipToPadding设置为false Check the gif in this SO answer . 检查此SO答案中的gif。

<android.support.v7.widget.RecyclerView
    android:id="@+id/task_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="5dp"
    android:clipToPadding="false" />

This will allow you to add the desired space at the beginning of scrollable content but prevent cropping the items. 这将使您可以在可滚动内容的开头添加所需的空间,但可以防止裁剪项目。

If the 5dp top margin isn't the problem, it may be the toolbar elevation, try to change 4dp to something much higher and check if changes your problem. 如果5dp的上边距不是问题,可能是工具栏高程,请尝试将4dp更改为更高的值,然后检查是否更改了问题。 Can you try same with margin top? 你可以用保证金顶试试吗? Also a negetive margin sometimes work. 有时也有消极作用。 Put a negetive margin on button of the toolbar and check. 在工具栏的按钮上放置一个空白,然后检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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