为什么我的片段没有显示在导航控制器中?
why my fragment doesn't show in the navigation controller?
-
android
-
android-navigation
-
android-architecture-components
-
android-jetpack
-
android-architecture-navigation
我是 Android 导航控制器的新手。
这是我的 xml activity:
<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>
我的科特林主要 Activity:
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"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
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-> {
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'
这里出了什么问题?
正如@Ranjan 为您指出的那样,这是您的导航片段的布局问题。它与导航 UI 无关。
使用:
app:layout_constraintBottom_toBottomOf="parent"
而不是:
app:layout_constraintBottom_toTopOf="parent"
在你的 NavControllerFragmnet 布局中。
android
android-navigation
android-architecture-components
android-jetpack
android-architecture-navigation
我是 Android 导航控制器的新手。
这是我的 xml activity:
<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>
我的科特林主要 Activity:
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"
app:layout_constraintBottom_toBottomOf="@+id/textView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
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-> {
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'
这里出了什么问题?
正如@Ranjan 为您指出的那样,这是您的导航片段的布局问题。它与导航 UI 无关。 使用:
app:layout_constraintBottom_toBottomOf="parent"
而不是:
app:layout_constraintBottom_toTopOf="parent"
在你的 NavControllerFragmnet 布局中。