【问题标题】:Android | Kotlin | PreferenceFragmentCompat overlaps BottomNavigationView安卓 |科特林 | PreferenceFragmentCompat 与 BottomNavigationView 重叠
【发布时间】:2020-08-06 21:05:07
【问题描述】:

在我的应用中显示我的偏好时遇到问题。 我的布局有一个 BottomNavigationView,并且首选项与此导航重叠。 我在这里看到过类似的问题,有不同的解决方案,但没有一个对我有用。我在布局的工作方式上一定遗漏了一些东西,但我没有看到。

这是我的主布局:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:weightSum="100"
    android:id="@+id/fragmentContainer"
    android:background="@color/colorSecondary"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:orientation="vertical"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigation"
        app:layout_constraintEnd_toStartOf="@+id/bottomNavigation"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigation"
        app:menu="@menu/bottom_nav_menu"
        android:background="@color/colorPrimary"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

这就是我加载偏好的方式(我没有这个布局):

class SettingsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        (activity as AppCompatActivity).supportActionBar?.title = "Settings"
        activity!!?.setTheme(R.style.PreferenceScreen);

        setPreferencesFromResource(R.xml.preferences,rootKey)
    }
}

这是它在应用程序中的外观,首选项显示在底部导航上方。有什么方法可以限制偏好显示直到底部导航视图,而不是进一步(或在它之上)?

我通过 MainActivity 中的调用按如下方式实例化 SettingsFragment:

replaceFragment(SettingsFragment(), "settings")

方法是:

private fun replaceFragment(fragment: Fragment, fragmentTag: String){
        val fragmentTransaction = supportFragmentManager.beginTransaction()
        fragmentTransaction.replace(R.id.fragmentContainer, fragment, fragmentTag)
        fragmentTransaction.commit()
    }

添加也有用: 我只在运行 Android 4.2.2 (JellyBean) 的三星 Galaxy S2 Plus 手机上看到这个问题,我也可以在运行 JellyBean(API 级别 17)的虚拟设备中模拟它。

我在运行 Android 10(Q,API 级别 29)的三星 S9 或运行 Android 5.0.2(Lollipop,API 级别 21)的 Alcatel Go Play 上看不到它。

所以我猜我正在对适用于较新 Android 版本但不适用于 API 级别 17 的布局做一些事情。

在我的 build.gradle 文件中,我的 defaultConfig 中有以下内容:

minSdkVersion 17
targetSdkVersion 29

【问题讨论】:

  • 你是如何实例化SettingsFragment的?
  • @karllindmark :我已经更新了我的帖子,其中包含有关如何实例化 SettingsFragment 的信息。
  • 添加了有关可以正常工作的设备以及无法正常工作的设备的更多信息。我怀疑是 API 级别问题,但不知道如何修复它,或者如果布局与我设置的 minSdkVersion 不兼容,为什么 Android Studio 不会抱怨它。这是我的第一个应用程序开发,需要学习很多东西,因此非常感谢任何有关处理此问题的帮助或建议。
  • 设置打开时是否应该显示底部导航?
  • @karllindmark :是的,否则用户无法从设置页面转到应用程序的其他部分。我来自 web 开发者背景,所以我可能对应用程序的设计方式有误。

标签: android kotlin bottomnavigationview overlapping preferencefragment


【解决方案1】:

这显然是 API 级别的问题。 我玩过虚拟设备,一旦我使用 API 级别 21,它就可以正常工作。所有低于此的 API 级别都会导致布局问题。

由于 API 级别 21 仍然覆盖了大约 94% 的设备,我采取了简单的方法,将我的 minSdkVersion 从 17 升级到了 21。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 1970-01-01
    • 2022-07-22
    • 2023-02-23
    相关资源
    最近更新 更多