片段工具栏上方的空白空白。如何解决?

时间:2018-12-03 06:41:57

标签: android android-fragments android-actionbar android-linearlayout android-scrollview

我正在编写一个基于片段的Android应用程序,该片段被插入root emty活动中。 我为每个片段配置单独的工具栏。当我打开第一个屏幕片段时,它工作正常。此屏幕的xml,工具栏代码和屏幕截图

documentation

<?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打开下一个片段时。此屏幕的屏幕截图和代码:

<?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)
    }
}

在第二个屏幕中可以看到,工具栏上方有一个奇怪的空间。这就是我的问题,我不知道如何解决。请说明如何解决。

add_contact_toolbar的PS布局

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

1 个答案:

答案 0 :(得分:0)

这可能与使用“ android:fitsSystemWindows =” true“有关,因为您的布局考虑了状态栏的高度。请尝试摆弄该值。