Fragment 的工具栏上方出现奇怪的空白 space。如何解决?
Strange empty white space above Fragment's toolbar. How to fix it?
我正在编写一个 Android 应用程序,它是基于片段的,片段被插入到根空 activity 中。
对于每个片段,我都配置了单独的工具栏。当我打开第一个 screen-fragment 时,它工作正常。本画面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_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_toolbar_main"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listOfClientsWithActions"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:clipToPadding="false" android:paddingTop="8dp"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAddContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="match_parent"
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_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/white"
android:paddingBottom="8dp"
android:orientation="vertical">
<include
android:id="@+id/toolbar_add_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_toolbar_add_contact"/>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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)
}
}
正如您在第二个屏幕上看到的,工具栏上方有一个奇怪的 space。所以这是我的问题,我不知道如何解决。请说明如何解决它。
P.S 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_width="match_parent"
android:layout_height="?attr/actionBarSize"
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"/>
这可能与 "android:fitsSystemWindows="true" 的使用有关,因为您的布局考虑了状态栏的高度。尝试摆弄该值。
我正在编写一个 Android 应用程序,它是基于片段的,片段被插入到根空 activity 中。 对于每个片段,我都配置了单独的工具栏。当我打开第一个 screen-fragment 时,它工作正常。本画面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_width="match_parent"
android:layout_height="match_parent"
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_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/toolbar_main"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_toolbar_main"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/listOfClientsWithActions"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:clipToPadding="false" android:paddingTop="8dp"/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabAddContact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="match_parent"
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_height="match_parent"
android:layout_width="match_parent"
android:background="@android:color/white"
android:paddingBottom="8dp"
android:orientation="vertical">
<include
android:id="@+id/toolbar_add_contact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
layout="@layout/layout_toolbar_add_contact"/>
<ScrollView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="wrap_content"
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)
}
}
正如您在第二个屏幕上看到的,工具栏上方有一个奇怪的 space。所以这是我的问题,我不知道如何解决。请说明如何解决它。
P.S 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_width="match_parent"
android:layout_height="?attr/actionBarSize"
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"/>
这可能与 "android:fitsSystemWindows="true" 的使用有关,因为您的布局考虑了状态栏的高度。尝试摆弄该值。