Listener Home和HomeAsUp按钮不适用于支持操作栏

时间:2014-10-14 19:52:42

标签: android android-actionbar-compat

我只是使用AppCompact v7中的支持操作栏。我写了bar.setDisplayHomeAsUpEnabled(true)并希望听这个按钮,但是在听众中:

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();
    switch (id) {
    case R.id.action_settings:
        return true;
    case R.id.HomeAsUp:
        return true;
    default:
        break;
    } 

    return super.onOptionsItemSelected(item);
}

当我想从它中找到项目时,方法不返回任何内容。如何使用支持操作栏听这个按钮?

1 个答案:

答案 0 :(得分:1)

    R.id.home - res id 2131034117
    R.id.homeAsUp - res id 2131034132
    android.R.id.home - res id 16908332  <-- HomeAsUpIndicator

ActionBar.setHomeAsUpIndicator(getResIdFromAttribute(this, R.attr.homeAsUpIndicator));
ActionBar.setDisplayHomeAsUpEnabled(true);

public static int getResIdFromAttribute(final Activity activity, final int attr) {
    if (attr == 0) return 0;
    final TypedValue typedvalueattr = new TypedValue();
    activity.getTheme().resolveAttribute(attr, typedvalueattr, true);
    return typedvalueattr.resourceId;
}