【问题标题】:support action bar title and icon in each fragment支持每个片段中的操作栏标题和图标
【发布时间】:2021-11-22 22:42:12
【问题描述】:

我正在开发 Kotlin,我使用底部导航活动创建了仪表板,其中创建了三个片段。 但我面临的问题是:在每个片段的操作栏上都没有显示标题,我还添加了一个图标,点击后转到另一个活动,但即使我的图标也没有显示在操作栏上。

这是我的DashbordActivity.kt 代码

package com.example.havesroad.activities.ui.activities

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.example.havensroad.R
import com.example.havensroad.databinding.ActivityDashboardBinding
import com.example.havesroad.activities.ui.fragments.DashboardFragment
import com.example.havesroad.activities.ui.fragments.OrdersFragment
import com.example.havesroad.activities.ui.fragments.ProductsFragment
import kotlinx.android.synthetic.main.activity_dashboard.*

class DashboardActivity : BaseActivity() {

    private lateinit var binding: ActivityDashboardBinding

    private val dashboardFragment = DashboardFragment()
    private val productsFragment = ProductsFragment()
    private val ordersFragment = OrdersFragment()

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

        setContentView(R.layout.activity_dashboard)
        changeFragments(dashboardFragment)


        nav_view.setOnNavigationItemSelectedListener {
            when(it.itemId){
                R.id.navigation_products -> changeFragments(productsFragment)
                R.id.navigation_dashboard -> changeFragments(dashboardFragment)
                R.id.navigation_orders -> changeFragments(ordersFragment)

            }
            true
        }

    }


    private fun changeFragments(fragment: Fragment){

        if(fragment !=null){

            val transaction = supportFragmentManager.beginTransaction()
            transaction.replace(R.id.nav_host_fragment, fragment)
            transaction.commit()
        }
        
    }
    
    override fun onBackPressed() {
        doubleBackToExit()
    }

}

下面是我的 DashboardFragment.kt 代码

package com.example.havesroad.activities.ui.fragments

import android.content.Intent
import android.os.Bundle
import android.view.*
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.havensroad.R
import com.example.havesroad.activities.ui.activities.SettingsActivity

class DashboardFragment : Fragment() {



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setHasOptionsMenu(true)
    }



    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        //  dashboardViewModel = ViewModelProviders.of(this).get(DashboardViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val textView: TextView = root.findViewById(R.id.text_dashboard)

        textView.text = "This is dashboard Fragment"

        return root

    }




    override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
        inflater.inflate(R.menu.dashboard_menu, menu)
        super.onCreateOptionsMenu(menu, inflater)
    }


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

            R.id.action_settings -> {

                startActivity(Intent(activity, SettingsActivity::class.java))
                return true
            }
        }
        return super.onOptionsItemSelected(item)
    }
    
}


下面是我的 OrdersFragment.kt 代码

package com.example.havesroad.activities.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.havensroad.R


class OrdersFragment : Fragment() {

 

    override fun onCreateView(

        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
      //  notificationsViewModel = ViewModelProviders.of(this).get(NotificationsViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_orders, container, false)
        val textView: TextView = root.findViewById(R.id.text_orders)

            textView.text = "This is orders Fragment"

        return root
    }
}


下面是我的 ProductsFragment.kt 代码


package com.example.havesroad.activities.ui.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import com.example.havensroad.R


class ProductsFragment : Fragment() {




    override fun onCreateView(

        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        homeViewModel = ViewModelProviders.of(this).get(HomeViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_products, container, false)
        val textView: TextView = root.findViewById(R.id.text_home)

            textView.text = "This is products Fragment"

        return root
    }


}

下面是我的 DashboardFragment xml 代码

<?xml version="1.0" encoding="utf-8"?>


<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize"
    android:background="@color/Hrgreen1">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"

        android:background="@color/Hrgreen1"
        app:itemIconTint="@color/Hrwhite"
        app:itemTextColor="@color/Hrwhite"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu"/>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

和我的片段 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.havesroad.activities.ui.fragments.DashboardFragment"
    android:background="@color/Hrwhite">




    <TextView
        android:id="@+id/text_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />




</androidx.constraintlayout.widget.ConstraintLayout>




这是我的 mobile_navigation.xml 文件


<?xml version="1.0" encoding="utf-8"?>
<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/mobile_navigation"
    app:startDestination="@+id/navigation_dashboard">

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.example.havesroad.activities.ui.fragments.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_products"/>

    <fragment
        android:id="@+id/navigation_products"
        android:name="com.example.havesroad.activities.ui.fragments.ProductsFragment"
        android:label="@string/title_products"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_orders"
        android:name="com.example.havesroad.activities.ui.fragments.OrdersFragment"
        android:label="@string/title_orders"
        tools:layout="@layout/fragment_orders" />
</navigation>


【问题讨论】:

  • 您好,您能否将代码共享到您的视图 xml 文件,特别是仪表板 xml 和片段的 xml 之一
  • 已经附上
  • 最后,请提供您的导航图 xml。 mobile_navigation.xml
  • 请阅读我在下面提交的答案,并通过接受让我知道它是否有效,如果无效,请告诉我,以便我进一步提供帮助
  • mobile_navigation.xml 附件

标签: android android-studio kotlin android-fragments


【解决方案1】:

由于您使用的是 Android Jetpack 库中的导航组件,因此您可以使用 android_label="Title"in mobile_navigation.xml 设置顶部栏上的标题:

<navigation>
    <fragment ...
              android:label="Page title">
      ...
    </fragment>
</navigation>

您可以从这里阅读更多内容:doc link

另外,将活动 xml 中的工具栏配置为

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="?attr/actionBarSize"
    android:background="@color/Hrgreen1">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
    put constraints />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"

        android:background="@color/Hrgreen1"
        app:itemIconTint="@color/Hrwhite"
        app:itemTextColor="@color/Hrwhite"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu"/>

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

在你的活动中onCreate():

val appBarConfiguration = AppBarConfiguration(navController.graph)


 val navController = findNavController(R.id.nav_host_fragment)
    val appBarConfiguration = AppBarConfiguration(navController.graph)
    findViewById<Toolbar>(R.id.toolbar)
        .setupWithNavController(navController, appBarConfiguration)

【讨论】:

  • from val appBarConfiguration = AppBarConfiguration(setOf(R.id.main, R.id.profile)) 我应该使用哪个ids?
  • 您好,很抱歉这个错误,实际上您需要在此处传递当前导航控制器的图表。我已经更新了答案,你能检查一下吗
猜你喜欢
  • 1970-01-01
  • 2011-07-13
  • 1970-01-01
  • 2016-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-13
相关资源
最近更新 更多