简体   繁体   English

Android工具栏中的自定义图标

[英]Custom icon in Android toolbar

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"
    />

Just tried it myself and the issue seems to be that you have to call setNavigationIcon after setSupportActionBar(toolbar) . 只是自己尝试过,问题似乎是你必须在setSupportActionBar(toolbar)之后调用setNavigationIcon Otherwise it'll only show the arrow as you've described. 否则它只会显示你所描述的箭头。
So to fix this issue just change the code like this: 所以要修复这个问题,只需更改代码如下:

//...
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. 注意:设置标题,contentDescription等同样如此。我不知道这是一个错误还是它的意图,但它确实有点奇怪。

In case you want to change menu icon . 如果您想更改菜单图标 (maybe somebody will need it) (也许有人需要它)

  1. In your activity 在你的活动中

     @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu_info, menu); return true; } 
  2. in your menu folder in res. 在你的菜单文件夹中。 (menu_info.xml) (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> 

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);
}
ActionBar.setHomeAsUpIndicator(...);

这个对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM