片段显示不正确

时间:2021-06-05 11:28:34

标签: android android-fragments

我正在学习 Kotlin,之前用 Java 开发过应用程序。我现在有一个带有 ToolbarNavigationViewFragmentView 的活动。据我了解: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。没有成功,我真的认为这是非常基本的,应该有效。当我运行调试器时,我可以看到在 Fragment 中调用了 onCreateView,这是应该的。

这就是它的样子。有时,文本会在屏幕上闪烁。所以似乎有什么东西覆盖了这个片段?

screenshot 1 screenshot 2

编辑 使用海拔 FragmentContainerView 更新 9dp 后,将显示文本。但是现在工具栏是隐藏的,尽管 FragmentContainerView 有一个 marginTop。 Screenshot 3

1 个答案:

答案 0 :(得分:1)

我想在这里建议两件事。

  • 如果可能的话,首先回到 ConstraintLayout,就像你上次的回答一样。肯定没有必要,但会推荐。
  • 其次,闪烁可能是由 elevation 的不同引起的,因为 elevationNavigationView 不同。

所以我建议您将以下内容添加到您的 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 视图(如 BottomNav 等)都高达 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>