问题描述
最近,我发现什么是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.
这篇关于AppCompatActivity与AppCompatDelegate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!