为什么我的片段没有显示在导航控制器中?

时间:2019-03-04 06:25:22

标签: android android-architecture-components android-navigation android-jetpack android-architecture-navigation

我是Android导航控制器的新手。

这是我主要活动的xml:

<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".Activities.MainActivity">


    <androidx.appcompat.widget.Toolbar
            android:id="@+id/main_activity_toolbar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:elevation="4dp" app:layout_constraintHorizontal_bias="0.0">
    </androidx.appcompat.widget.Toolbar>



    <fragment
            android:id="@+id/nav_main_host_fragment"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/main_activity_toolbar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:name="androidx.navigation.fragment.NavHostFragment"
            app:navGraph="@navigation/main_graph"
            app:defaultNavHost="true"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

我的kotlin主要活动:

class MainActivity : AppCompatActivity(),NavController.OnDestinationChangedListener {

    lateinit var navController : NavController

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

        navController = Navigation.findNavController(this,R.id.nav_main_host_fragment)
        navController.addOnDestinationChangedListener(this)

        setupActionBar()
    }

    override fun onDestinationChanged(controller: NavController, destination: NavDestination, arguments: Bundle?) {

    }

    private fun setupActionBar() {
        setSupportActionBar(main_activity_toolbar)
        supportActionBar?.title = null


        val appBarConfiguration = AppBarConfiguration(
            setOf(
                // set some destination as the top hierarchy destination, to make the up button doesn't show.
                R.id.destination_latestMessage
            ))

        NavigationUI.setupWithNavController(main_activity_toolbar,navController,appBarConfiguration)
    }


}

我的片段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=".Fragments.LatestMessageFragment"
                                                   android:id="@+id/ConstraintLayout_latestMessage">

    <!-- TODO: Update blank fragment layout -->
    <TextView
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="latest message"
            android:id="@+id/textView" app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"/>
    <Button
            android:text="Button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/button" android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="@+id/textView" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginStart="8dp" app:layout_constraintEnd_toEndOf="@+id/textView"
            android:layout_marginEnd="8dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

我的片段kotlin文件:

class LatestMessageFragment : Fragment() {

    lateinit var mContext : Context
    lateinit var mActivity : FragmentActivity
    lateinit var fragmentView : View

    override fun onAttach(context: Context) {
        super.onAttach(context)

        mContext = context
        activity?.let { mActivity = it }

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        fragmentView = inflater.inflate(R.layout.fragment_latest_message, container, false)
        setHasOptionsMenu(true) // to setup custom menu on the action bar
        return fragmentView
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)


    }

    override fun onResume() {
        super.onResume()


    }

    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {

        inflater.inflate(R.menu.log_out_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }


    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        val id = item.itemId

        return when (id) {
            R.id.logout-> {
//
//                val loginDestination = LatestMessageFragmentDirections.actionToLoginDestination()
//                Navigation.findNavController(fragmentView).navigate(loginDestination)

                true
            }

            else -> super.onOptionsItemSelected(item)
        }
    }




}

这是图形xml文件:

<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/main_graph"
            app:startDestination="@id/destination_latestMessage">

    <fragment android:id="@+id/destination_latestMessage" android:name="com.lakuin.cs.Fragments.LatestMessageFragment"
              android:label="Latest Message" tools:layout="@layout/fragment_latest_message"/>
</navigation>

如您所见,在我的目标片段上,它具有按钮和文本视图,但是当我运行该应用程序时,文本视图和按钮没有出现在屏幕上。

我只有一个片段,但是该片段没有显示在屏幕上,但是我设置的工具栏及其菜单出现在屏幕上

我在下面使用专一性

//Navigation controller
implementation 'android.arch.navigation:navigation-fragment-ktx:1.0.0-rc02'
implementation 'android.arch.navigation:navigation-ui-ktx:1.0.0-rc02'

这里出了什么问题?

1 个答案:

答案 0 :(得分:1)

这是您的导航片段的布局问题,因为@Ranjan为您指出。它与导航UI无关。 使用:

app:layout_constraintBottom_toBottomOf="parent"

代替:

app:layout_constraintBottom_toTopOf="parent"

在NavControllerFragmnet布局中。