ActionBarSherlock菜单没有显示ForceOverflow

时间:2014-05-26 04:13:44

标签: java android android-actionbar actionbarsherlock overflow

我的问题是,当您打开应用程序时,操作栏中的溢出菜单未显示。 如果您看到MainActivity类我有一个onOptionsItemSelected用于导航器,那么当我在底部创建一个onCreatedOptionMenu及其各自的onOptionsItemSelected失败时。 我不知道如何解决它。

这就是我想要的

Image

但是当我打开应用程序时,操作栏中没有任何内容

这是我的 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:

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);
}

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>

如果您需要更多信息可以回答,请问我 (我还没有发布图片给你看我的应用程序)

对不起,如果我在发帖时犯了错误,也是因为我的英语,我注意到我是初学者。


修改

我认为这解决了菜单没有出现在带有此代码的操作栏上并删除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;
}

并在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();
    }

现在我有类似 This

的内容

不知道在操作栏上生成菜单是一个图标而不是像#34; Type&#34;这样的文字。

现在这是我的问题。

1 个答案:

答案 0 :(得分:0)

这是您需要对项目标签

进行的操作
 android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"]

始终会显示菜单,永远不会让您的菜单项进入溢出模式..

设置

android:showAsAction="never"

查看here以获取更多说明。