簡體   English   中英

從工具欄刪除默認邊距底部

[英]remove default margin bottom from Toolbar

在我的應用程序中,我將android.support.v7.widget.Toolbar用於工具欄。

這是我的布局

<?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"/>

<?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" />

我嘗試在工具欄和視圖的其余部分之間添加一些空間。 為此,我在marginTop中將5dp添加到了視圖下方的布局中。 這就導致了這個問題:在滾動視圖時,工具欄下方還有一些空間。

滾動之前

開始滾動后

在我的樣式文件xml中,沒有用於邊距或填充的樣式。 如何在工具欄和其余工具之間添加一些空間而又不會觸發此問題(滾動視圖時)?

原因

基於xml文件,在我看來:

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

是裁剪RecyclerView內容的原因。

當您設置layout_marginTop之間AppBarLayout和上部邊緣ConstraintLayout出現5dp高空白。

您可以通過為開發人員打開Android選項之一來確認這一點: Show Layout Bounds

對於您想要實現的效果,請嘗試刪除android:layout_marginTop並將RecyclerView設置為android:paddingTop ,但還要將android:clipToPadding設置為false 檢查此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" />

這將使您可以在可滾動內容的開頭添加所需的空間,但可以防止裁剪項目。

如果5dp的上邊距不是問題,可能是工具欄高程,請嘗試將4dp更改為更高的值,然后檢查是否更改了問題。 你可以用保證金頂試試嗎? 有時也有消極作用。 在工具欄的按鈕上放置一個空白,然后檢查。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM