简体   繁体   English

Android操作栏菜单项被强制溢出

[英]Android action bar menu items getting forced to overflow

I have looked at the following stack overflow articles to figure out why my action bar items are getting forced into overflow. 我查看了以下堆栈溢出文章,以找出为什么我的操作栏项目被强制溢出。

I have also tried replacing "ifRoom" with "always," and I'm not getting any console output about issues find the drawable resources (they even show correctly in the menu preview pane) 我也试过用“always”替换“ifRoom”,并且我没有得到关于问题的任何控制台输出找到可绘制的资源(它们甚至在菜单预览窗格中正确显示)

main.xml 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>

from MainActivity.java 来自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);
}

Here's what it looks like: 这是它的样子:

动作栏项目未显示

And here's my res directory: 这是我的res目录:

/ RES /

Use android:showAsAction instead of app:showAsAction . 使用android:showAsAction而不是app:showAsAction

And when the error Should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto showed up I got a suggestion to suppress and it worked!!! 当错误Should use app:showAsAction with the appcompat library with xmlns:app="schemas.android.com/apk/res-auto出现我有一个建议抑制,它的工作!

Use this line>> tools:ignore="AppCompatResource" and add xmlns:tools="http://schemas.android.com/tools" to top. 使用这一行>> tools:ignore="AppCompatResource"并将xmlns:tools="http://schemas.android.com/tools"到顶部。

对于你所有的app:属性,也有一个等效的android:属性(例如, app:showAsActionandroid:showAsAction )。

If your activity extends AppCompatActivity and targetSdk is > 22 and you use com.android.support:appcompat-v7 make sure you use in your xml: 如果您的活动扩展AppCompatActivity并且targetSdk> 22并且您使用com.android.support:appcompat-v7,请确保在xml中使用:

app:showAsAction="ifRoom"

instead of 代替

android:showAsAction="ifRoom"

If you use android: the showAsAction is actually ignored and the items will always go to the overflow menu. 如果您使用android:showAsAction实际上被忽略,项目将始终转到溢出菜单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM