Not able to get the overflow feature for actionbar in android

谁都会走 提交于 2019-12-13 07:47:26

问题


What i am trying to do ::

What is happening ::


My code::

actionbar_sort_menu.xml

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

    <item
        android:id="@+id/searchID"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="ifRoom|withText"
        android:title="Search"/>
    <item
        android:id="@+id/featuredID"
        android:icon="@drawable/ic_action_featured"
        android:showAsAction="ifRoom|withText"
        android:title="Featured"/>
    <item
        android:id="@+id/dealsID"
        android:icon="@drawable/ic_action_deal"
        android:showAsAction="ifRoom|withText"
        android:title="Deals"
        />
    <item
        android:id="@+id/inviteID"
        android:icon="@drawable/ic_action_share"
        android:showAsAction="ifRoom|withText"
        android:title="Invite or Share"/>

</menu>

Manifest code::

<application
        android:name="com.myApp.utilities.AppController"
        android:configChanges="orientation|keyboardHidden"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

Theme i am using::

android:Theme.Holo

Support library ::

android-support-v4.jar

{EDIT}

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

    <item
        android:id="@+id/searchID"
        android:icon="@drawable/ic_action_search"
        yourapp:showAsAction="never"
        android:title="Search"/>
    <item
        android:id="@+id/featuredID"
        android:icon="@drawable/ic_action_featured"
        yourapp:showAsAction="never"
        android:title="Featured"/>
    <item
        android:id="@+id/dealsID"
        android:icon="@drawable/ic_action_deal"
        yourapp:showAsAction="never"
        android:title="Deals"
        />
    <item
        android:id="@+id/inviteID"
        android:icon="@drawable/ic_action_share"
        yourapp:showAsAction="never"
        android:title="Invite or Share"/>

</menu>

error i am getting

No resource identifier found for attribute 'showAsAction' in package com.myApp


回答1:


Since you are using Support library you need to have your own namespace prefix

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

Then change this

  android:showAsAction="ifRoom|withText"

to

  yourapp:showAsAction="ifRoom|withText"

Note : If there is not enough space to display items in the actionbar it will appear in the overflow menu. If you have room for few items it appears on the actionbar and the rest in overflow.

Edit:

yourapp:showAsAction="never" 

From the docs

http://developer.android.com/guide/topics/resources/menu-resource.html

 never  Never place this item in the Action Bar.   



回答2:


  • Well This was just a workaround to get the answer as below
  • Since i am not having the device to test for the solution to test Raghunandan answer
  • I still believe Raghunandan answer holds good & also sharing what i came to do

Reason for me to test on mobile because:: "device has a menu-button, the overflow-icon won't show."


Code::

@Override
public boolean onOptionsItemSelected(MenuItem item) {   
    ft = getFragmentManager().beginTransaction();

    switch(item.getItemId()) {
    case R.id.descriptionID:
        onItemChangeBackground(1);

        Fragment fragContents = FrgBufDetails.newInstance(buf_off_id);
        ft.replace(R.id.content_frame, fragContents);
        ft.addToBackStack(null);
        ft.commit();

        return true;
    case R.id.MapID:
        onItemChangeBackground(2);

        Fragment fragMap = FrgBufLocation.newInstance(buf_off_id);
        ft.replace(R.id.content_frame, fragMap);
        ft.addToBackStack(null);
        ft.commit();

        return true;
    case R.id.galleryID:
        onItemChangeBackground(3);

        Fragment fragGallery = FrgBufGallery.newInstance(buf_off_id);
        ft.replace(R.id.content_frame, fragGallery);
        ft.addToBackStack(null);
        ft.commit();

        return true;
    case R.id.inviteID:
        onItemChangeBackground(4);

        View title = (View) getActivity()
                    .findViewById(R.id.inviteID);

        //Creating the instance of PopupMenu
        PopupMenu popup = new PopupMenu(getActivity(), title);
        //Inflating the Popup using xml file
        popup.getMenuInflater().inflate(R.menu.actionbar_sort_menu, popup.getMenu());
        popup.show();//showing popup menu

        return true;
    }
    return super.onOptionsItemSelected(item);
}


来源:https://stackoverflow.com/questions/24404286/not-able-to-get-the-overflow-feature-for-actionbar-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!