Android 工具栏中的自定义图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26449082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Custom icon in Android toolbar
提问by Antoine
I'm trying to use define a custom icon in the support Toolbar but the only icon shown is a left arrow... I tried to set it in the layout and programmatically but the result is the same.
我试图在支持工具栏中使用定义一个自定义图标,但显示的唯一图标是一个左箭头......我试图在布局中以编程方式设置它,但结果是一样的。
Here is my Activity
这是我的活动
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("");
setSupportActionBar(toolbar);
}
And my toolbar layout
还有我的工具栏布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
app:navigationIcon="@drawable/ic_action_bar"
android:minHeight="@dimen/action_bar_size"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
/>
回答by reVerse
Just tried it myself and the issue seems to be that you have to call setNavigationIconafter setSupportActionBar(toolbar). Otherwise it'll only show the arrow as you've described.
So to fix this issue just change the code like this:
我自己试了一下,问题似乎是你必须setNavigationIcon在setSupportActionBar(toolbar). 否则,它只会显示您所描述的箭头。
所以要解决这个问题,只需像这样更改代码:
//...
Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setTitle("");
Note:Same goes for setting the title, contentDescription etc. I don't know if this a bug or if it is intended, but it's definitely kinda strange.
注意:设置标题、内容描述等也是如此。我不知道这是一个错误还是有意为之,但这确实有点奇怪。
回答by Hyman The Ripper
In case you want to change menu icon. (maybe somebody will need it)
如果您想更改菜单图标。(也许有人会需要它)
In your activity
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_info, menu); return true; }in your menu folder in res. (menu_info.xml)
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_info_action" android:icon="@drawable/ic_info_icon" android:title="@string/information" app:showAsAction="ifRoom"/> </menu>
在您的活动中
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_info, menu); return true; }在 res 的菜单文件夹中。(menu_info.xml)
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_info_action" android:icon="@drawable/ic_info_icon" android:title="@string/information" app:showAsAction="ifRoom"/> </menu>
回答by James Semaj
The current most efficient way to achieve this:
目前实现这一目标的最有效方法:
first display the left hand side icon correctly, call this function in onCreateView or onCreate:
首先正确显示左侧图标,在 onCreateView 或 onCreate 中调用此函数:
protected void enableDisplayHomeAsHome(boolean active) {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(active); // switch on the left hand icon
actionBar.setHomeAsUpIndicator(R.drawable.ic_action_home); // replace with your custom icon
}
}
Now you can intercept this button press in your activity:
现在您可以在您的活动中拦截此按钮按下:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home: { //index returned when home button pressed
homeButtonPressed();
return true;
}
}
return super.onOptionsItemSelected(item);
}
回答by alcsan
ActionBar.setHomeAsUpIndicator(...);
This one works for me.
这个对我有用。

