2015-07-16 27 views
11

Tôi có kết quả tiếp theo:SearchView không mở rộng đầy đủ chiều rộng

enter image description here

menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/action_search" 
     android:actionLayout="@layout/my_search_view" 
     android:icon="@android:drawable/ic_menu_search" 
     android:title="Search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="always|collapseActionView"/> 

</menu> 

my_search_view.xml

<?xml version="1.0" encoding="utf-8"?> 
<SearchView xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:maxWidth="10000dp"/> 

Hoạt động

@Override 
public boolean onCreateOptionsMenu(Menu menu) 
{ 
    getMenuInflater().inflate(R.menu.menu, menu); 
    MenuItem searchItem = menu.findItem(R.id.action_search); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem); 
    searchView.setQueryHint("Поиск"); 
    searchView.setOnQueryTextListener(this); 


    // Get the SearchView and set the searchable configuration 
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 

    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 


    ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); 
    searchView.setLayoutParams(params); 
    searchView.setIconified(false); 

    MenuItemCompat.expandActionView(searchItem); 
    return super.onCreateOptionsMenu(menu); 
} 

Tôi muốn rằng X (hành động đóng) sẽ nằm ở bên phải của màn hình.

+0

mục đích của android: maxWidth = "10000dp" là gì? –

+0

@AlexiosKarapetsas là một nỗ lực liên quan đến http://stackoverflow.com/a/23451789/2956344 –

+0

Bản sao có thể có của [SearchView in OptionsMenu not full width] (http://stackoverflow.com/questions/18063103/searchview-in- tùy chọn-không-đầy đủ chiều rộng) – Benjamin

Trả lời

0

Tôi gặp sự cố tương tự. Chế độ xem tìm kiếm không được mở rộng hoàn toàn. Sau đây là cách tôi giải quyết vấn đề

trong styles.xml của tôi được xác định một phong cách cho SearchView

<style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView"> 
    <item name="queryBackground">@color/light_gray_3</item> 
    <item name="submitBackground">@color/gray</item> 
    <item name="closeIcon">@android:drawable/ic_menu_close_clear_cancel</item> 
    <item name="goIcon">@drawable/abc_ic_go_search_api_mtrl_alpha</item> 
    <item name="searchHintIcon">@null</item> 
    <item name="android:maxWidth">1000dp</item> 
</style> 

sau đó trong phong cách của tôi cho thanh công cụ tôi đã thêm một dòng phong cách hóa SearchView

<style name="ThemeToolbar" parent="Theme.AppCompat.Light"> 
    <item name="colorControlNormal">@color/gray</item> 
    <item name="colorPrimary">@android:color/white</item> 
    <item name="android:textColorSecondary">@color/gray</item> 
    <item name="searchViewStyle">@style/MySearchViewStyle</item> 
</style> 

Đây là bố cục tìm kiếm của tôi được xác định trong search_view_layout.xml

<android.support.v7.widget.SearchView xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res/rk.chkout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
app:searchHintIcon="@null" 
android:maxWidth="10000dp" /> 

và tuyên bố mục tìm kiếm trong menu.xml

<item 
    android:orderInCategory="2" 
    android:id="@+id/action_search" 
    android:icon="@drawable/search_icon" 
    android:title="@string/search" 
    app:actionLayout="@layout/search_view_layout" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    app:showAsAction="always|collapseActionView"/> 
22

SearchView MenuItemCompat của có một tài sản mang tên maxWidth. Bí quyết sau đây:

searchView.setMaxWidth(Integer.MAX_VALUE); 
Các vấn đề liên quan