gpt4 book ai didi

android - 如何在其 setSupportActionBar 时在操作栏中添加图标

转载 作者:太空狗 更新时间:2023-10-29 15:35:56 28 4
gpt4 key购买 nike

我有以下设置工具栏的代码

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.ListView);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);

tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
//downloadImage();
}

我应该添加这段代码吗?

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);

我想在工具栏操作栏中有图标怎么办?

最佳答案

是的,你应该添加

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
//and this to handle actions
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

要添加图标,到menu目录下的main.xml中添加

<item android:id="@+id/action_settings" 
android:title="@string/action_settings"
android:icon="@drawable/icon"
android:orderInCategory="100"
app:showAsAction="always" />

关于android - 如何在其 setSupportActionBar 时在操作栏中添加图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33328486/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com