persencesCreen总是显示出额外空间的顶部

发布于 2025-01-28 08:35:49 字数 4129 浏览 4 评论 0原文

目前,我面临着有关显示首选项片段的问题。我正在使用底部导航菜单,我想显示一个工具栏(以前的操作栏),显示导航栏的当前选择的菜单项。

问题在于,所需的工具栏和偏好片段的顶部之间似乎有第二个无形工具栏,如图像所示。

首选项屏幕的顶部:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="@string/preferences_category_online_area">
        <EditTextPreference
            android:key="preference_username"
            android:inputType="text"
            android:persistent="true"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/edit_text_preferences_username_summary"
            android:title="@string/edit_text_preferences_username_text" />
        <EditTextPreference
            android:key="preference_password"
            android:inputType="textPassword"
            android:persistent="true"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/edit_text_preferences_passwort_summary"
            android:title="@string/edit_text_preferences_passwort_text" />
...

Mobile_navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_library">

    <fragment
        android:id="@+id/navigation_dummy"
        android:name="de.example.OnlineSelectionFragment"
        android:label="@string/title_dummy"
        tools:layout="@layout/fragment_dummy" />

    <fragment
        android:id="@+id/navigation_preferences"
        android:name="de.example.PrefsFragment"
        android:label="@string/title_preferences" />
</navigation>

class preffsfragment.kt class ,其中片段填充了偏好

package de.example

import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat


class PrefsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, null)
    }
}

如何删除空的顶部空间?以前,prefsfragment已在额外的活动中显示,但是没有它,我无法将其格式化为XML。

非常感谢!

编辑1 根据要求的主要活动XML文件:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

Currently, I'm facing a problem regarding the display of the preference fragment. I'm using a bottom navigation menu and I want to show a toolbar (previously action bar) showing the currently selected menu item of the navigation bar.

The problem is that there seems to be a second invisible toolbar between the wanted toolbar and the top of the preference fragment as seen in the image.

Top space in preference fragment visible by swiping down

The top of the preference screen:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="@string/preferences_category_online_area">
        <EditTextPreference
            android:key="preference_username"
            android:inputType="text"
            android:persistent="true"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/edit_text_preferences_username_summary"
            android:title="@string/edit_text_preferences_username_text" />
        <EditTextPreference
            android:key="preference_password"
            android:inputType="textPassword"
            android:persistent="true"
            android:selectAllOnFocus="true"
            android:singleLine="true"
            android:summary="@string/edit_text_preferences_passwort_summary"
            android:title="@string/edit_text_preferences_passwort_text" />
...

The mobile_navigation.xml:

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/mobile_navigation"
    app:startDestination="@+id/navigation_library">

    <fragment
        android:id="@+id/navigation_dummy"
        android:name="de.example.OnlineSelectionFragment"
        android:label="@string/title_dummy"
        tools:layout="@layout/fragment_dummy" />

    <fragment
        android:id="@+id/navigation_preferences"
        android:name="de.example.PrefsFragment"
        android:label="@string/title_preferences" />
</navigation>

Class PrefsFragment.kt in which the fragment is populated with the preferences

package de.example

import android.os.Bundle
import androidx.preference.PreferenceFragmentCompat


class PrefsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, null)
    }
}

How can I remove the empty top space? Previously, the PrefsFragment has been shown within an extra activity but without it I cannot format it in XML.

Thank you very much!

Edit 1
As requested the main activity xml file:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七七 2025-02-04 08:35:49

问题已经解决。

行为的原因是android:paddingtop =“?attr/actionbarsize”在主活动xml文件中。

Problem has been solved.

The reason for the behaviour was the android:paddingTop="?attr/actionBarSize" in the main activity xml file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文