本文介绍了AppCompatActivity与AppCompatDelegate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我发现什么是AppCompatDelegate,所以自然而然地出现了一个问题-它比使用AppCompatActivity差吗?或者它们之间有区别?它们是什么?

Recently I discovered what is AppCompatDelegate so a natural question arises - is it worse than using AppCompatActivity or do they have their differences and what are they?

推荐答案

如果您的活动类未扩展AppCompatActivity,但您仍要使用其某些功能,则可以使用AppCompatDelegate.

If your activity class is not extending AppCompatActivity but you still want to use some of its features then you can you AppCompatDelegate.

您可以在活动类中使用以下代码行创建Appcompatdelegate:

You can create Appcompatdelegate with following lines of code in your activity class:

private AppCompatDelegate getDelegate() {
    if (mDelegate == null) {
       mDelegate = AppCompatDelegate.create(this, null);
    }
    return mDelegate;
}

以下是示例,如果您想在活动中添加工具栏,但您的班级没有扩展AppCompatActivity.

Here is the example if you want to add Toolbar in your activity but your class is not extending AppCompatActivity.

https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3

这篇关于AppCompatActivity与AppCompatDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-18 09:04