Android操作栏设置选项未在真实设备上显示

时间:2015-07-08 03:30:39

标签: android android-actionbar

我有操作栏,有后退按钮和设置选项。当我在emmulator上运行app时,它会显示后退按钮和设置选项。 像 enter image description here

但在真实设备上它不显示设置选项 像 enter image description here

请告诉我如何解决它?

这是我的活动代码

 public class AddNewDish extends ActionBarActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_new_dish);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option, menu);
        return super.onCreateOptionsMenu(menu);

    }

这是菜单xml代码

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">


<item android:id="@+id/action_settings"
    android:title="@string/action_settings"
    android:orderInCategory="1"
    android:showAsAction="never" />
</menu>

1 个答案:

答案 0 :(得分:1)

将以下代码放在 ActionBarActivity onCreate()上:

try {
  ViewConfiguration config = ViewConfiguration.get(this);
  Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");

  if (menuKeyField != null) {
    menuKeyField.setAccessible(true);
    menuKeyField.setBoolean(config, false);
  }
}
catch (Exception e) {

}

完成