如何在Android Studio中使用setSupportActionBar函数?

huangapple go评论74阅读模式
英文:

How to use setSupportActionBar function in android studio?

问题

我已经在 XML 布局中创建了工具栏(Toolbar)。然后,我用以下代码在 Java 中进行了初始化:

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
}}

如您在上述代码中所见,我尝试使用 setSupportActionBar(Toolbar toolbar);,但是出现了错误。我还尝试了将其写为 setSupportActionBar(toolbar);,但两者都不起作用。

如何正确使用此函数?在这里应该放置哪些参数?

英文:

I've created toolbar in xml layout. Then I've initialized it in java with the following code:

public class MainActivity extends AppCompatActivity {

private Toolbar toolbar;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    toolbar = findViewById(R.id.toolbar);
    //setSupportActionBar(Toolbar toolbar);
}}

As you can see in the code above I tried to setSupportActionBar(Toolbar toolbar);, but I got an error. Also I've tried write it as setSupportActionBar(toolbar); and both didn't work. <br><br>What is way to use this function correctly? Which arguments should I put here?

答案1

得分: 0

我已经找到了答案。如果你遇到同样的问题,那么你应该使用import android.support.v7.widget.Toolbar;代替import android.v7.Toolbar;

英文:

Actually I've found an answer. If you have the same problem, then you should put import android.support.v7.widget.Toolbar; instead of import android.v7.Toolbar;

huangapple
  • 本文由 发表于 2020年9月15日 18:28:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63899944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定