如何在Kotlin片段活动类中创建自定义操作栏

问题描述 投票:0回答:2

我正在尝试从Android Studio的底部导航活动模板中自定义一个片段类和一个活动类,以生成一个自定义的操作栏,但是当我在模拟器上运行该应用程序时,该自定义的操作栏不会出现。

这里是使用initializeCustomActionBar()方法的自定义活动类:

class CountriesActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_countries)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        navView.setItemIconTintList(null);

        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_england, R.id.navigation_scotland, R.id.navigation_wales, R.id.navigation_nireland
            )
        )
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    fun initializeCustomActionBar() {

        val actionBar: android.app.ActionBar? = getActionBar()
        actionBar?.displayOptions = DISPLAY_SHOW_CUSTOM
        actionBar?.setCustomView(R.layout.custom_action_bar)
        actionBar?.setDisplayShowCustomEnabled(true)
        actionBar?.show()

    }
}

这是定制的片段类,它调用initializeCustomActionBar()方法:

class EnglandFragment : Fragment() {

    private lateinit var englandViewModel: EnglandViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        englandViewModel =
            ViewModelProviders.of(this).get(EnglandViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_england, container, false)
        val textView: TextView = root.findViewById(R.id.text_england)
        englandViewModel.text.observe(this, Observer {
            textView.text = it

            var mContext : FragmentActivity?  = FragmentActivity()
            mContext = this!!.getActivity() as CountriesActivity
            mContext.initializeCustomActionBar()

        })
        return root
    }
}

android android-studio kotlin android-actionbar
2个回答
0
投票

您只需要使用活动实现的接口。

首先在自己的文件中定义接口,例如CountriesActivityListener.kt

interface CountriesActivityListener {
    fun initializeCustomActionBar()
}

在活动中实现界面:

class CountriesActivity : AppCompatActivity(), CountriesActivityListener {

    //........

    override fun initializeCustomActionBar() {
        val actionBar: android.app.ActionBar? = actionBar
        actionBar?.let {
            it.displayOptions = DISPLAY_SHOW_CUSTOM
            it.setCustomView(R.layout.custom_action_bar)
            it.show()
        }
    }
}

使用片段中的接口:

class EnglandFragment : Fragment() {

    private lateinit var englandViewModel: EnglandViewModel

     // Define the reference to the interface listener:
    private lateinit var contriesActivityListener: CountriesActivityListener

    // Setup the interface listener in the onAttach override:
    override fun onAttach(context: Context) {
        contriesActivityListener = activity as CountriesActivityListener
        super.onAttach(context)
    }

    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        englandViewModel =
                ViewModelProviders.of(this).get(EnglandViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_england, container, false)
        val textView: TextView = root.findViewById(R.id.text_england)
        englandViewModel.text.observe(this, Observer {
            textView.text = it

            // Use the interface method:
            contriesActivityListener.initializeCustomActionBar()
        })
        return root
    }
}

0
投票

我将initializeCustomActionBar()编辑为:

fun initializeCustomActionBar() {

    val actionBar: ActionBar? = this.supportActionBar
    actionBar?.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM)
    actionBar?.setDisplayShowCustomEnabled(true)
    actionBar?.setCustomView(R.layout.custom_action_bar)
}
© www.soinside.com 2019 - 2024. All rights reserved.