Kotlin:用于导航抽屉supportFragmentManager的片段无法按预期替换布局

时间:2018-03-17 13:51:57

标签: android android-fragments kotlin navigation-drawer

这是MainActivity.kt的代码,我在这个应用程序中使用导航抽屉

fun displayScreen(id: Int) {

    val fragment = when (id) {

        R.id.nav_home -> {

            HomeFragment()
        }
        R.id.nav_favourite -> {

            FavouriteFragment()
        }
        R.id.nav_About -> {
            AboutFragment()
        }
        R.id.nav_Feedback -> {
            FeedbackFragment()
        }
        else -> {
            HomeFragment()
        }

    }
  

直到现在代码工作正常。我想

     supportFragmentManager.beginTransaction().replace(R.id.r1, 
      fragment).commit()
  

替换布局后,Objects会自动移动到左上角         角

}

override fun onNavigationItemSelected(item: MenuItem): Boolean {

    when (item.itemId) {
        R.id.nav_home -> {
            displayScreen(item.itemId)
            supportActionBar?.setTitle("Home")
        }
        R.id.nav_favourite -> {
            displayScreen(item.itemId)
            supportActionBar?.setTitle("Favourite")
        }
        R.id.nav_About -> {
            displayScreen(item.itemId)
            supportActionBar?.setTitle("About")
        }
        R.id.nav_Feedback -> {
            displayScreen(item.itemId)
            supportActionBar?.setTitle("Feedback")
        }

    }
    displayScreen(item.itemId)


    drawer_layout.closeDrawer(GravityCompat.START)
    return true
}
  

预期

enter image description here

  

获得

enter image description here

  

content_main

<RelativeLayout 
     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/r1"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     app:layout_behavior="@string/appbar_scrolling_view_behavior"
     tools:context="com.example.shikh.kotlinnavigationdrawer.MainActivity"
     tools:showIn="@layout/app_bar_main">

</RelativeLayout>
  

fragment_home

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Home"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"/>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView5"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        android:text="Click" />

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

删除constraint属性,将您的活动布局更改为FrameLayout and remove the app:layout_behavior =&#34; @ string / appbar_scrolling_view_behavior&#34; part since it is not inside a CoordinatorLayout`。< / p>