gpt4 book ai didi

android - 为什么我的 fragment 没有显示在导航 Controller 中?

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:53 24 4
gpt4 key购买 nike

我是 Android 导航 Controller 的新手。

这是我的主要 Activity 的 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 主要 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)
}
}

我的 fragment 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>

我的 fragment 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>

如您所见,在我的目标 fragment 上,它有按钮和 TextView ,但是当我运行应用程序时, TextView 和按钮没有出现在屏幕上。

我只有一个 fragment ,但是那个 fragment 没有显示在屏幕上,但是我设置的工具栏和它的菜单出现在屏幕上

我使用下面的依赖项:

//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 为您指出的那样,这是您的导航 fragment 的布局问题。它与导航用户界面无关。使用:

app:layout_constraintBottom_toBottomOf="parent"

代替:

app:layout_constraintBottom_toTopOf="parent"

在您的 NavControllerFragmnet 布局中。

关于android - 为什么我的 fragment 没有显示在导航 Controller 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54977665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com