删除searchView图标

时间:2014-06-25 09:17:18

标签: android android-actionbar

在我的应用中,我在操作栏中使用SearchView,它总是像这样展开:

enter image description here

我的main.xml是这样的:

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

我想删除搜索图标并展开editText。

此致

1 个答案:

答案 0 :(得分:0)

您应该知道可折叠的操作项具有最大宽度。我能想到以下几点:

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

您还可以使用以下方式设置颜色:

mSearchView.setBackgroundColor(Color.BLUE);