操作栏按钮仅显示在溢出中

时间:2015-05-06 18:50:37

标签: android android-actionbar android-actionbaractivity

我尝试将带图标的按钮添加到操作栏,因此我按照Android开发人员page上的信息进行操作。但是,尽管已经分配了图标,但按钮只会显示在溢出而不是条形本身。我希望按钮出现在栏上,带有图标。我的代码:

活动的onOptionsItemSelected方法

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_new:
        launchAddRuleActivity();
        return true;
    case R.id.action_settings:
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

活动的onCreateOptionsMenu

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

操作栏布局的main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mhmt.autotextmate.Main" >

<!-- New, should appear as action button -->
<item
    android:id="@+id/action_new"
    android:icon="@drawable/ic_action_new"
    android:showAsAction="ifRoom"
    android:title="@string/action_new"/>
<!-- Settings, should always be in the overflow -->
<item
    android:id="@+id/action_settings"
    android:icon="@drawable/ic_action_save"
    android:showAsAction="never"
    android:title="@string/action_settings"/>

最后,目标和最小SDK:

<uses-sdk
    android:minSdkVersion="11"
    android:targetSdkVersion="22" />

我从here得到了drawables,所以大小不应该是个问题。此类确实扩展ActionBarActivity,并导入android.support.v7.app.ActionBarActivity。我目前正在使用运行Android 5.1.1且分辨率为720x1280的Google Nexus模拟器进行测试。

我一定是做错了什么,有人能看到我缺少用他们的图标显示按钮,而不是溢出吗?

1 个答案:

答案 0 :(得分:3)

试试这个app:showAsAction="always"

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.mhmt.autotextmate.Main" >

<!-- New, should appear as action button -->
<item
android:id="@+id/action_new"
android:icon="@drawable/ic_action_new"
app:showAsAction="always"
android:title="@string/action_new"/>
<!-- Settings, should always be in the overflow -->
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_action_save"
app:showAsAction="always"
android:title="@string/action_settings"/>