【问题标题】:Listener Home and HomeAsUp buttons not works on Support Action BarListener Home 和 HomeAsUp 按钮在支持操作栏上不起作用
【发布时间】:2014-10-14 19:52:41
【问题描述】:

我只是使用 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);
}

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

【问题讨论】:

  • 代替 R.id.HomeAsUp,试试 android.id.R.home
  • 谢谢。这可行,但是 HomeAsUp 按钮呢?
  • 我从未将 HomeAsUp 视为有效的 android.R.id...这是您自己创建的 id 吗?
  • 不,当我选择 R.id -> 它有 HomeAsUp。据我了解,它是带箭头的主页按钮。我曾认为,这两个不同的按钮。

标签: android android-actionbar-compat


【解决方案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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    相关资源
    最近更新 更多