简体   繁体   English

不显示ActionBarSherlock菜单ForceOverflow

[英]ActionBarSherlock menu ForceOverflow not showing

My problem is that when you open the app is not shown the overflow menu in the actionbar. 我的问题是,当您打开应用程序时,操作栏中的溢出菜单未显示。 If you see en the MainActivity class I have a onOptionsItemSelected for the navigationdrawer, so when I create on the bottom a onCreatedOptionMenu with their respective onOptionsItemSelected fails. 如果您看到MainActivity类,则为导航抽屉设置了onOptionsItemSelected,因此在底部创建带有各自onOptionsItemSelected的onCreatedOptionMenu时会失败。 and I dont know how to fix it. 而且我不知道如何解决它。

This is what I want 这就是我要的

Image 图片

But nothing appears in the actionbar when I open application 但是,当我打开应用程序时,动作栏中什么也没有出现

This is my menu.xml : 这是我的menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
style="@style/Theme.Sherlock">
<item 
    android:id="@+id/more"
    android:icon="@drawable/ic_action_overflow"
    android:title="@string/more"
    android:showAsAction="always"/>

    <menu >
        <item
            android:id="@+id/contacto"
            android:title="@string/contacto"
            android:showAsAction="always|withText"/>
        <item
            android:id="@+id/recomenda"
            android:title="@string/recomenda"
            android:showAsAction="always|withText"/>
        <item
            android:id="@+id/salir"
            android:title="@string/salir"
            android:showAsAction="always|withText"/>

        </menu>

MainActivity.java: MainActivity.java:

public class MainActivity extends SherlockFragmentActivity {

// Declare Variables
DrawerLayout mDrawerLayout;
ListView mDrawerList;
ActionBarDrawerToggle mDrawerToggle;
MenuListAdapter mMenuAdapter;
String[] title;
Fragment fragment0 = new Fragment0();
//20 more

private CharSequence mDrawerTitle;
private CharSequence mTitle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from drawer_main.xml
    setContentView(R.layout.drawer_main);

    // Get the Title
    mTitle = mDrawerTitle = "¿Qué buscás?";

    // Generate title
    title = new String[] { " ASD ", "CORTE LÁSER", "CORTE METALES",
            "CORTE POR CHORRO DE AGUA", "CURSOS", "EQUIPOS DE VIDEO", "FICHAS TÉCNICAS", 
            "FOTÓGRAFOS", "GRÁFICAS", "IMPRESIÓN 3D", "LIBRERÍAS Y PAPELERAS", "MAQUETAS Y PROTOTIPOS",
            "MODELADO 3D", "MODELOS", "PLÁSTICOS", "ROUTER", "SUBLIMACIÓN", "TELGOPOR", "TERMOFORMADO",
            "TORNERO MADERA", "TORNERO METALES", "VINILOS" };

    // Locate DrawerLayout in drawer_main.xml
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // Locate ListView in drawer_main.xml
    mDrawerList = (ListView) findViewById(R.id.listview_drawer);

    // Set a custom shadow that overlays the main content when the drawer
    // opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
            GravityCompat.START);

    // Pass string arrays to MenuListAdapter
    mMenuAdapter = new MenuListAdapter(MainActivity.this, title);

    // Set the MenuListAdapter to the ListView
    mDrawerList.setAdapter(mMenuAdapter);

    // Capture listview menu item click
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    // Enable ActionBar app icon to behave as action to toggle nav drawer
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
            R.drawable.ic_drawer, R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            // TODO Auto-generated method stub
            super.onDrawerClosed(view);
        }

        public void onDrawerOpened(View drawerView) {
            // TODO Auto-generated method stub
            // Set the title on the action when drawer open
            getSupportActionBar().setTitle(mDrawerTitle);
            super.onDrawerOpened(drawerView);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        selectItem(0);
    }
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {

        if (mDrawerLayout.isDrawerOpen(mDrawerList)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    }

    return super.onOptionsItemSelected(item);
}

// ListView click listener in the navigation drawer
private class DrawerItemClickListener implements
        ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        selectItem(position);
    }
}

private void selectItem(int position) {

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    // Locate Position
    switch (position) {
    case 0:
        ft.replace(R.id.content_frame, fragment0);
        ft.addToBackStack(null);

        break;

    case 1:
        ft.replace(R.id.content_frame, fragment1);
        ft.addToBackStack(null);
        break;

    // and 19 more cases
    }

    ft.commit();
    mDrawerList.setItemChecked(position, true);
    // Get the title followed by the position
    setTitle(title[position]);
    // Close drawer
    mDrawerLayout.closeDrawer(mDrawerList);

}

public boolean onCreatedOptionMenu(Menu menu) {

    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.menu1, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    // Pass any configuration change to the drawer toggles
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void setTitle(CharSequence title) {
    mTitle = title;
    getSupportActionBar().setTitle(mTitle);
}

And the Manifest.xml: Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />
//...

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/Theme.Sherlock.Light.ForceOverflow" >
    <meta-data
     //...
</application>

Please if you need some more information to answer, ask me (I have not yet allowed to post images to show you my app) 请如果您需要更多信息来回答,请问我(我尚未允许发布图片来向您展示我的应用)

Sorry if I made a mistake when posting, also for my English, I notice that I am beginner coding. 抱歉,如果我在发布时出错了,也因为我的英文,我注意到我是初学者编码。


EDIT: 编辑:

I think that solves the fact that the menu does not appear on the actionbar with this code and deleting the menu.xml: 我认为这可以解决以下问题,即菜单不会通过此代码显示在操作栏上,并删除了menu.xml:

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    SubMenu subMenu = menu.addSubMenu("Más");

    subMenu.add("Volver");
    subMenu.add("Contacto");
    subMenu.add("Salir");

    subMenu.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    return true;
}

And added this in the onOptionsItemSelected already have in the MainActivity.java: 并将其添加到MainActivity.java中已经拥有的onOptionsItemSelected中:

if (item.getTitle().toString().equalsIgnoreCase("Publicá!")) {
        Intent in = new Intent(getApplicationContext(),Publica.class);
        startActivity(in);
        Toast.makeText(this, "Publicá tu Negocio/Local/Emprendimiento", Toast.LENGTH_LONG).show();


    } if (item.getTitle().toString().equalsIgnoreCase("Contacto")) {
        Intent in = new Intent(getApplicationContext(),Contacto.class);
        startActivity(in);
        Toast.makeText(this, "Contactate y reportanos ...", Toast.LENGTH_LONG).show();
    }

     if (item.getTitle().toString().equalsIgnoreCase("Salir")) {
         Intent i = new Intent(); i.setAction(Intent.ACTION_MAIN); 
            i.addCategory(Intent.CATEGORY_HOME); i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
            startActivity(i); android.os.Process.killProcess(android.os.Process.myPid());
        Toast.makeText(this, "<-- Busca lo que necesitas", Toast.LENGTH_LONG).show();
    }

Now i Have something like This 我现在已经像


But not know to generate the menu on the actionbar be an icon instead of text like "Type". 不知道要在操作栏上生成菜单,是图标而不是诸如“类型”之类的文本。

That is my problem now. 那是我现在的问题。

This is what you need to do with the item tags 这是您需要使用项目标签进行的操作

 android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

always will always show you menus, never will let your menu item to come in the overflow mode.. 始终将始终向您显示菜单,永远不会使您的菜单项进入溢出模式。

set

android:showAsAction="never"

look here for more description. 在这里查看更多说明。

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

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