简体   繁体   中英

Remove searchView icon

In my app, I'm using a SearchView in the actionbar that it's always expanded like this:

在此处输入图片说明

My main.xml is this:

<item
    android:id="@+id/menu_search"
    android:actionViewClass="android.widget.SearchView"
    android:showAsAction="always"
    android:title="Search" />

MainActivity.java:

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem searchViewItem = menu.findItem(R.id.menu_search);
    SearchView searchView = (SearchView) searchViewItem.getActionView();
    searchView.setIconifiedByDefault(false);

    return true;
}

I would like to remove the search icon and expand the editText.

Regards

You should know that action items that can be collapsed have a max width. I can think of the following:

public class GreatAndroid extends FragmentActivity {
private SearchView mSearchView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActionBar().setDisplayShowHomeEnabled(false);
    getActionBar().setDisplayShowTitleEnabled(false);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.menu_search, menu);
    MenuItem mi = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) mi.getActionView();
    mSearchView.setIconifiedByDefault(false);

    /**
    You can use Display.getSize(Point) from API 13 onwards.
    For API 11 and 12, use Display.getWidth()
    **/

    final Point p = new Point();

    getWindowManager().getDefaultDisplay().getSize(p);

    // Create LayoutParams with width set to screen's width
    LayoutParams params = new LayoutParams(p.x, LayoutParams.MATCH_PARENT);

    mSearchView.setLayoutParams(params);

    return true;
}
}

You can also set a color, using :

mSearchView.setBackgroundColor(Color.BLUE);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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