我已經看了下面的堆棧溢出文章,找出爲什麼我的操作欄項目被強制溢出。Android操作欄菜單項被強制溢出
- Can't find the ActionBar resource icon
- Action Items not showing in ActionBar with showAsAction="ifRoom"
- ActionBar Action Items not showing
我也試圖與替換 「ifRoom」 「永遠」,我不得到有關問題的任何控制檯輸出找到繪製資源(它們甚至可以在菜單預覽窗格中正確顯示)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/menu_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="ifRoom|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView"
android:title="Search"/>
<item android:id="@+id/menu_new"
android:icon="@drawable/ic_action_new"
app:showAsAction="ifRoom"
android:title="New"
android:orderInCategory="0"/>
</menu>
從MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
final ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
}
@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;
}
@Override
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();
return super.onOptionsItemSelected(item);
}
這裏是什麼樣子:
這是我的res目錄:
這個問題解決了它,但Android Studio突出顯示了android:showsAsACtion屬性現在有一個錯誤。我可以忽略它,但只是想知道爲什麼這是需要的 –
@ToddAnderson:什麼是錯誤? – CommonsWare
「應使用app:showAsAction與appcompat庫與xmlns:應用程序=」http://schemas.android.com/apk/res-auto「 –