【问题标题】:Disable collapse button in actionbar - Android禁用操作栏中的折叠按钮 - Android
【发布时间】:2016-09-25 14:55:40
【问题描述】:

我对操作栏中的按钮有疑问。那么为什么我在操作栏中创建按钮它只会崩溃从不显示图标。这是我的菜单

<item
    android:id="@+id/action_name"
    android:icon="@drawable/icon_home"
    android:title="Home"
    app:showAsAction="ifRoom"
    android:orderInCategory="0"/>

这是我的屏幕:

【问题讨论】:

  • 你能详细解释一下吗?
  • 是的,但我希望它显示图标主页
  • 显示活动代码。
  • MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.actionbar_items, menu); return super.onCreateOptionsMenu(menu);
  • 和 onOptionSelected: int id = item.getItemId(); if (id == R.id.action_name) { Intent intent = new Intent(IndexActivity.this,HomeActivity.class);开始活动(意图);结束(); } return super.onOptionsItemSelected(item);

标签: android android-studio button android-activity android-actionbar


【解决方案1】:

试试看,

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="schemas.android.com/apk/res/android"
      xmlns:app="schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_name"
    android:icon="@drawable/icon_home"
    android:title="Home"
    app:showAsAction="always"
    android:orderInCategory="1" /> </menu>

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            case R.id.action_name:
                //your code//
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

【讨论】:

  • schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/tools">
  • 检查更新的答案。
  • 现在,再试一次,尝试用我的答案替换你的整个代码并通知我。
  • 我尝试替换您的代码,但它总是崩溃。
猜你喜欢
  • 2013-03-03
  • 1970-01-01
  • 2017-08-03
  • 1970-01-01
  • 2016-12-13
  • 1970-01-01
  • 2013-10-30
  • 2016-04-29
相关资源
最近更新 更多