Android SearchView默认设置为Expanded

ber*_*kie 11 android android-layout searchview android-studio

我目前正在开发一个简单的应用程序,使用Android Studio,最低API级别为8,现在我正在使用android.support.v7.widget.SearchViewLinearLayoutcontent_main.xml布局,而不是ACTIONBAR,现在问题是,我无法将searchView设置为Expanded默认情况下,我已经寻找解决方案,我只有这个:

android:iconifiedByDefault="false"

这是我的searchView的完整代码

          <android.support.v7.widget.SearchView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/searchView"
            android:background="#00000000"
            android:iconifiedByDefault="false"
            android:layout_weight="1" />
Run Code Online (Sandbox Code Playgroud)

但没有运气,它不起作用.谁有人可以帮助我?任何帮助,将不胜感激.非常感谢!

ber*_*kie 33

好的,所以我解决了这个问题.对于想要在SearchView中使用支持库的任何人,您可以在此处自定义它.

          <android.support.v7.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            app:closeIcon="@drawable/ic_clear" // To set custom clear icon

            app:searchIcon="@drawable/ic_search" //To set custom search icon

            app:queryBackground="@color/transparent" // i decided to remove underline so i create a custom background for my searchView

            app:queryHint="@string/filterHint" //to set a custom Hint


            app:iconifiedByDefault="false" //And finally, to Expand it as default

            />
Run Code Online (Sandbox Code Playgroud)

正如我所说,我害怕在运行时通过java代码覆盖xml布局,所以为了使它在运行时组织并且不再覆盖,这就是我的工作方式.

参考

感谢帮助!!!


Tha*_*ana 6

我使用以下代码扩展了searchview并将光标同时放置:

final SearchView sv = new SearchView(((MainActivity) getActivity()).getSupportActionBar().getThemedContext());
        sv.setIconifiedByDefault(true);
        sv.setFocusable(true);
        sv.setIconified(false);
        sv.clearFocus();
        sv.requestFocusFromTouch();
Run Code Online (Sandbox Code Playgroud)