以编程方式自定义SearchView

3
我想自定义应用程序中搜索视图小部件中的“edittext”。我想以编程方式设置其背景可绘制对象。我还想通过编程方式添加标志 android:imeOptions="flagNoExtractUi",以便在横向模式下,软键盘仅覆盖屏幕的一半。
请问如何在搜索视图的“edittext”上进行这些自定义?

请查看此链接:https://dev59.com/e2gu5IYBdhLWcg3wxZzd#11669808 - Ahmed Ekri
谢谢Ahmed,一旦我有了视图,我该如何以编程方式设置ime选项android: imeOptions =“flagNoExtractUi”? - nmvictor
尝试一下。youView.setImeOptions(flagNoExtractUi); - Ahmed Ekri
1个回答

1

这是我所做的事情。我研究了ActionBarSherlock库中的abs_search_view.xml,并了解到R.id.search_plate代表一个LinearLayout。LinearLayout的第一个子元素是一个视图,其类为com.actionbarsherlock.widget.SearchView$SearchAutoComplete。因此,我检索了第一个子元素,并将其强制转换为com.actionbarsherlock.widget.SearchView.SearchAutoComplete,进行了空值检查,随后使用searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI)设置了输入法选项,正如评论中Ahmend所建议的那样。希望这能对某些人有所帮助。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接