操作栏按钮仅显示在溢出中
Action bar buttons only showing in the overflow
我试图将带有图标的按钮添加到操作栏,所以我遵循了 android 开发人员 page 上的信息。然而,尽管已分配图标,但按钮只会显示在溢出中,而不会显示在栏本身上。我希望按钮与其图标一起出现在栏上。我的代码:
activity
的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);
}
}
activityonCreateOptionsMenu
@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 那里得到了可绘制对象,所以大小应该不是问题。 class 确实扩展了 ActionBarActivity
,并导入了 android.support.v7.app.ActionBarActivity
。我目前正在 Google Nexus 模拟器 运行 Android 5.1.1 上进行测试,分辨率为 720x1280。
我一定是做错了什么,谁能看到我缺少什么来显示带有图标的按钮,而不是在溢出中?
试试这个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"/>
我试图将带有图标的按钮添加到操作栏,所以我遵循了 android 开发人员 page 上的信息。然而,尽管已分配图标,但按钮只会显示在溢出中,而不会显示在栏本身上。我希望按钮与其图标一起出现在栏上。我的代码:
activity
的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);
}
}
activityonCreateOptionsMenu
@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 那里得到了可绘制对象,所以大小应该不是问题。 class 确实扩展了 ActionBarActivity
,并导入了 android.support.v7.app.ActionBarActivity
。我目前正在 Google Nexus 模拟器 运行 Android 5.1.1 上进行测试,分辨率为 720x1280。
我一定是做错了什么,谁能看到我缺少什么来显示带有图标的按钮,而不是在溢出中?
试试这个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"/>