英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论