首页 > 解决方案 > 片段未正确显示

问题描述

我正在学习 Kotlin,以前用 Java 做过应用程序。我现在有一个Toolbar,NavigationView和 a的活动FragmentView。据我了解:Fragment Tutorial,在 xml 文件中声明 Fragment 就足够了。

我有这个activity_main.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
    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/navigation_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true">
        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/main_app_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/AppTheme.AppBarOverlay"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/main_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/holo_blue_light"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </com.google.android.material.appbar.AppBarLayout>

        <com.google.android.material.navigation.NavigationView
            android:id="@+id/main_navigation"
            android:layout_marginTop="?attr/actionBarSize"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/navigation_header"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/main_app_bar_layout"
            app:menu="@menu/navigation_menu" />

    <androidx.fragment.app.FragmentContainerView
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_container"
        android:name="com.example.android.camerax.tflite.TestFragment"
        tools:layout="@layout/fragment_test" />
</androidx.drawerlayout.widget.DrawerLayout>

这是fragment_test.xml

<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"
    tools:context="com.example.android.camerax.tflite.TestFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:textAlignment="center"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是Activity

class CameraActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
    private lateinit var fragment: CameraActivityFragment
    private lateinit var navigationDrawer: DrawerLayout
    private lateinit var toolbar: Toolbar

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_camera)

        navigationDrawer = findViewById(R.id.navigation_container)
        // Handle user consent update
        toolbar = findViewById(R.id.main_toolbar)
        setSupportActionBar(toolbar)

        val toggle = ActionBarDrawerToggle(
            this,
            navigationDrawer,
            toolbar,
            R.string.navigation_drawer_open,
            R.string.navigation_drawer_close
        )
        navigationDrawer.addDrawerListener(toggle)
        toggle.syncState()
        val navigationView: NavigationView = findViewById(R.id.main_navigation)
        navigationView.setNavigationItemSelectedListener(this)

    }

    override fun onNavigationItemSelected(item: MenuItem): Boolean {
        if (item.itemId == R.id.show_recordings) {
//             Start ListView
        }
        navigationDrawer.closeDrawer(GravityCompat.START)
        return true
    }
}

这是TestFragment.kt

class TestFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_test, container, false)
    }

    companion object {
        // TODO: Rename and change types and number of parameters
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            TestFragment().apply {
                arguments = Bundle().apply {
                }
            }
    }
}

我试图阅读上面的链接并遵循以下内容:AndroidTrivia。没有成功,我真的认为这是非常基本的,应该可以工作。当我运行调试器时,我可以看到它onCreateView在 Fragment 中被调用,因为它应该是。

这就是它的外观。有时,文本会在屏幕上闪烁。所以似乎有些东西正在覆盖片段?

截图1 截图2

编辑FragmentContainerView使用高度 更新后9dp,将显示文本。但是现在 Toolbar 被隐藏了,虽然FragmentContainerView. 截图 3

标签: androidandroid-fragments

解决方案


我想在这里提出两点建议。

  • 如果可能的话,首先回到ConstraintLayout你上一个答案中。这不是必须的,但会推荐。
  • elevation其次,由于elevationof不同,因此闪烁可能是由差异引起的NavigationView

所以我建议您将以下内容添加到您的FragmentContainerView

<androidx.fragment.app.FragmentContainerView
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/main_container"
        android:elevation="9dp"
        android:name="com.example.android.camerax.tflite.TestFragment"
        tools:layout="@layout/fragment_test" />

9dp因为大多数 Android 视图(如BottomNavetc)都取决于8dp

还建议您修改fragment_test如下并添加以下内容

<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:background="@android:color/white"
    android:clickable="true"
    android:focusable="true"
    tools:context="com.example.android.camerax.tflite.TestFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment"
        android:textAlignment="center"
        android:gravity="center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

推荐阅读