Fragment 工具栏上方奇怪的空白区域。如何解决?
Posted
技术标签:
【中文标题】Fragment 工具栏上方奇怪的空白区域。如何解决?【英文标题】:Strange empty white space above Fragment's toolbar. How to fix it? 【发布时间】:2019-05-04 10:45:52 【问题描述】:我正在编写一个 android 应用程序,它基于 Fragments,它被插入到根 emty 活动中。 对于每个片段,我都配置了单独的工具栏。当我打开第一个屏幕片段时,它工作正常。该界面的xml、工具栏代码和截图
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_
android:layout_
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical"
android:layout_
android:layout_>
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<include
android:id="@+id/toolbar_main"
android:layout_
android:layout_
layout="@layout/layout_toolbar_main"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listOfClientsWithActions"
android:layout_
android:scrollbars="vertical"
android:layout_
android:clipToPadding="false" android:paddingTop="8dp"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAddContact"
android:layout_
android:layout_
android:layout_gravity="end|bottom"
android:src="@drawable/ic_baseline_add_white_24"
android:layout_margin="16dp" app:fabSize="normal"/>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_
android:layout_
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:itemIconTint="@color/drawer_item"
app:menu="@menu/drawer_menu"
app:itemTextAppearance="@style/NavDrawerItem"
app:headerLayout="@layout/nav_header"/>
</android.support.v4.widget.DrawerLayout>
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
initToolbar(view)
private fun initToolbar(view: View)
val toolbar = view.findViewById<Toolbar>(R.id.toolbar_main)
(activity as AppCompatActivity).setSupportActionBar(toolbar)
val actionbar: ActionBar? = (activity as AppCompatActivity).supportActionBar
actionbar?.apply
setHasOptionsMenu(true)
setDisplayHomeAsUpEnabled(true)
setHomeAsUpIndicator(R.drawable.ic_baseline_menu_white_24)
title = getString(R.string.app_name)
然后当我通过推 FAB 打开下一个 Fragment 时。此屏幕截图和代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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_
android:layout_
android:background="@android:color/white"
android:paddingBottom="8dp"
android:orientation="vertical">
<include
android:id="@+id/toolbar_add_contact"
android:layout_
android:layout_
layout="@layout/layout_toolbar_add_contact"/>
<ScrollView
android:layout_
android:layout_
android:orientation="vertical">
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical">
<EditText
android:id="@+id/editTextName"
android:inputType="textPersonName"
android:hint="@string/name"
tools:text="Федоров Алексей"
android:theme="@style/CustomEditTextTheme"
style="@style/AddContactEditTextBasicStyle"/>
<EditText
android:id="@+id/editTextPhone"
android:digits="1234567890+-() "
android:inputType="number"
android:hint="@string/phone_number"
android:theme="@style/CustomEditTextTheme"
style="@style/AddContactEditTextBasicStyle"/>
<EditText
android:id="@+id/editTextEmail"
android:inputType="textEmailAddress"
android:hint="@string/email"
tools:text="aleksey.fedorovjob@mail.ru"
android:theme="@style/CustomEditTextTheme"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextClientType"
android:theme="@style/CustomEditTextTheme"
android:layout_
android:layout_
app:hintText="@string/client_type"
app:starterText="@string/seller"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextObjectType"
android:theme="@style/CustomEditTextTheme"
android:layout_
android:layout_
app:hintText="@string/object_type"
app:starterText="@string/apartments"
style="@style/AddContactEditTextBasicStyle"/>
<EditText
android:id="@+id/editTextAddress"
android:inputType="textPostalAddress"
android:hint="@string/address"
tools:text="пр. Новгородский, д. 158"
android:theme="@style/CustomEditTextTheme"
style="@style/AddContactEditTextBasicStyle"/>
<faranjit.currency.edittext.CurrencyEditText
android:id="@+id/editTextPrice"
android:inputType="numberDecimal"
android:hint="@string/price"
app:showSymbol="false"
app:groupDivider="."
app:monetaryDivider="."
tools:text="3.200.000"
android:theme="@style/CustomEditTextTheme"
style="@style/AddContactEditTextBasicStyle"/>
<EditText
android:id="@+id/editTextLocation"
android:inputType="textPostalAddress"
android:hint="@string/location"
tools:text="Московский район"
android:theme="@style/CustomEditTextTheme"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextWcCount"
android:layout_
android:layout_
android:theme="@style/CustomEditTextTheme"
android:paddingBottom="0dp"
app:hintText="@string/wc_count"
app:starterText="0"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextBrCount"
android:layout_
android:layout_
android:theme="@style/CustomEditTextTheme"
app:hintText="@string/br_count"
app:starterText="0"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextAction"
android:layout_
android:layout_
android:theme="@style/CustomEditTextTheme"
app:hintText="@string/action"
app:starterText="@string/call"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextDate"
android:layout_
android:layout_
android:theme="@style/CustomEditTextTheme"
app:hintText="@string/date"
app:starterText="01.01.1970"
style="@style/AddContactEditTextBasicStyle"/>
<com.winwin_tech.fixapp.fixapp.util.CustomEditText
android:id="@+id/editTextTime"
android:layout_
android:layout_
android:theme="@style/CustomEditTextTheme"
app:hintText="@string/time"
app:starterText="12:00"
style="@style/AddContactEditTextBasicStyle"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
initToolbar(view)
private fun initToolbar(view: View)
val toolbar = view.findViewById<Toolbar>(R.id.toolbar_add_contact)
(activity as AppCompatActivity).setSupportActionBar(toolbar)
val actionbar: ActionBar? = (activity as AppCompatActivity).supportActionBar
actionbar?.apply
setHasOptionsMenu(true)
setDisplayHomeAsUpEnabled(true)
正如您在第二个屏幕上看到的,工具栏上方有一个奇怪的空间。所以这是我的问题,我不知道如何解决它。请说明如何解决。
附上add_contact_toolbar的布局
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:layout_
android:layout_
android:elevation="4dp"
android:background="?attr/colorPrimary"
app:title="@string/add_contact"
app:titleTextAppearance="@style/ToolbarTitle"
app:titleTextColor="@android:color/white" app:navigationIcon="@drawable/ic_baseline_close_white_24"
tools:menu="@menu/menu_add_contact_screen"/>
【问题讨论】:
你能分享你的 layout_toolbar_add_contact 你是如何添加片段的? 我在导航库 Cicerone github.com/terrakok/Cicerone 中处理片段操作。它是著名的俄罗斯图书馆,它看起来像狐狸示例导体。我已经和作者讨论过这个库对我的问题的影响。好像不是这个原因 这可能与“android:fitsSystemWindows="true" 的使用有关,因为您的布局考虑了状态栏高度。尝试摆弄该值。 Mathias,我已经找到了解决方案,并考虑在主题样式中使用 fitSystemWIdow = true 属性。发表您的评论作为答案我会批准它 【参考方案1】:这可能与“android:fitsSystemWindows="true" 的使用有关,因为您的布局考虑了状态栏的高度。尝试摆弄该值。
【讨论】:
完美...拯救了我的一天以上是关于Fragment 工具栏上方奇怪的空白区域。如何解决?的主要内容,如果未能解决你的问题,请参考以下文章
Android Studio新建BottomNavigationActivity后,fragment上部区域有空白的解决方法
Android Studio新建BottomNavigationActivity后,fragment上部区域有空白的解决方法
Android Studio新建BottomNavigationActivity后,fragment上部区域有空白的解决方法