以编程方式自定义SearchView

时间:2013-10-14 10:06:09

标签: android android-edittext ime searchview

我想在我的应用程序中自定义搜索视图小部件的'edittext'。我想以编程方式设置其背景可绘制。我还想以编程方式添加标志android:imeOptions="flagNoExtractUi",以便在陆地景观模式下,软键盘只能覆盖屏幕的一半。

任何人都知道如何在搜索视图'edittext'上进行自定义吗?

The search view

1 个答案:

答案 0 :(得分:1)

这就是我所做的。我在动作栏sherlock库中研究了abs_search_view.xml并了解到R.id.search_plate代表了一个LinearLayour。 LinearLayout的第一个子类是一个类为com.actionbarsherlock.widget.SearchView$SearchAutoComplete的视图。因此,我检索了第一个孩子并将其转换为com.actionbarsherlock.widget.SearchView.SearchAutoComplete,进行了空检查,然后在评论中由Ahmend建议的searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI)设置了ime选项。希望这有助于某人。

public static void styleSearchView(SearchView searchView, Context context) {
    LinearLayout searchPlate = (LinearLayout) searchView
            .findViewById(R.id.abs__search_plate);
    // TODO
    // searchPlate.setBackgroundResource(R.drawable.your_custom_drawable);
    if (searchPlate != null)
        ((com.actionbarsherlock.widget.SearchView.SearchAutoComplete) searchPlate
                .getChildAt(0))
                .setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView
            .findViewById(R.id.abs__search_src_text);
    if (searchText != null)
        searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    // TODO
    // searchText.setHintTextColor(context.getResources().getColor(R.color.your_custom_color));
}

enter image description here