SherlockActionBar with AutoCompleteTextView and other items

≡放荡痞女 提交于 2019-12-08 10:55:49

问题


I want to have an action bar with AutoCompleteTextView(Collapsible) as a menuItem along with other menuItems like Delete. I am using SherlockActionBar's library to make it compatible with lower version(like 2.2, 2.3).

My requirement is to display

  1. a searchIcon (for collapsible AutoCompleteTextView) at extreme right of the actionBar and

  2. a delete icon to left of above mentioned searchIcon.

Now when user taps on searchIcon, it should expand itself to cover the entire actionBar (hidinig delete icon).

I had tried wrapping both the iconsearch(for AutoCompleteTextView) and deleteButton in a layout and providing this layout as a view for action bar provided by SherlockActionBar library but it didn't displayed anything.

Then I tried adding iconSearch (with attribute value as collapsible) and deleteButton as individual menu items (shown in the SherlockActionBar Samples) but it just displays actionSearch.

I have no idea how to implement the same using SherlockActionBar library. I have tried to implement the same by following the samples given but couldn't achieve this.

Any help would be appreciated...

Thanks in advance.


回答1:


First create your xml layout from search (layout_search.xml):

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <AutoCompleteTextView
        android:id="@+id/edit_search"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Search" >

        <requestFocus />
    </AutoCompleteTextView>

    <ImageView
        android:id="@+id/img_clear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
        android:src="@drawable/ic_menu_clear" />

</FrameLayout>

In your menu xml:

<item
    android:id="@+id/menu_search"
    android:icon="@drawable/ic_menu_search"
    android:showAsAction="always|collapseActionView"
    android:title="Search">
</item>

Creating menus:

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getSupportMenuInflater().inflate(R.menuyour_menu, menu);        
            menu.getItem(0).setActionView(R.layout.layout_search);
            AutoCompleteTextView search = (AutoCompleteTextView) mI.getActionView().findViewById(R.id.edit_search); 
            ImageView imgClear = (ImageView) menu.getItem(0).getActionView().findViewById(R.id.img_clear).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                search.setText("");
            }
        });

            return super.onCreateOptionsMenu(menu);
        }



回答2:


I think you should use ready-made solution com.actionbarsherlock.widget.SearchView.

menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menu_remove"
        android:icon="@drawable/content_remove"
        android:showAsAction="ifRoom"
        android:title="@string/menu_remove" />
    <item
        android:id="@+id/menu_search"
        android:actionViewClass="com.actionbarsherlock.widget.SearchView"
        android:icon="@drawable/action_search"
        android:showAsAction="always|collapseActionView"
        android:title="@string/search" />
</menu>

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the options menu from XML
    MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);

    return super.onCreateOptionsMenu(menu);
}

result: Android ActionBarSherlock Search http://img6.imageshack.us/img6/7448/androidsearchactionbar.png




回答3:


I am done with the required implementation. Thanks to Rafal Galka & andrehsouza. I had to combine both the solutions (provided by Rafal Galka & andrehsouza) to achieve the desired effect.

What I did is, I created a menu.xml with two action items. One of them is a standard action item with normal icon and title and another one is with the custom view. And this custom view is nothing but just a simple AutoCompleteTextView which has been defined under res/layout folder.

So here is my res/menu/menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/menu_xxx"
        android:showAsAction="ifRoom|withText"
        android:icon="@drawable/drawablexxx"
        android:title="@string/xxx"/>

    <item
        android:id="@+id/menu_search"
        android:actionLayout="@layout/collapsibleactionview"
        android:showAsAction="always|collapseActionView"
        android:title="@string/action_search"/>
</menu>

and collapsibleactionview.xml has been defined under res/layout as :

<?xml version="1.0" encoding="utf-8"?>
<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edittextcollapsible"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/hint search"
    android:imeOptions="actionSearch"
    android:inputType="text" />

Bingo !!!



来源:https://stackoverflow.com/questions/15947557/sherlockactionbar-with-autocompletetextview-and-other-items

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