2016-07-31 61 views
5

我通過菜單將搜索查看我的工具欄:搜索查看沒有填滿整個寬度

<?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:icon="@drawable/ic_search_white" 
     android:orderInCategory="0" 
     android:title="@android:string/search_go" 
     android:visible="true" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:iconifiedByDefault="false" 
     app:showAsAction="always" /> 
</menu> 

我不知道爲什麼,但搜索查看被移動到右側: enter image description here

任何想法爲什麼會發生?

回答

1

我有同樣的問題,試圖以編程方式補充一點:

searchView.setMaxWidth(Integer.MAX_VALUE); 

如果這不起作用嘗試將<item>標籤替換到<SearchView>

如果使用API​​ 20及以上的做到這一點
添加到您的styles.xml文件:

/*Theme name and parent can be different - depends on the one that already 
declared in you manifest under the tag <application> theme:AppTheme*/ 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">/
    <item name="searchViewStyle">@style/SearchView</item> 
</style> 

<style name="SearchView" parent="Widget.AppCompat.SearchView"> 
    <item name="android:maxWidth">@dimen/maxSize</item> 
</style> 
<dimen name="maxSize">1000dp</dimen> 
+0

我用searchView.setMaxWidth(Integer.MAX_VALUE);並使用而不是菜單和nothig作品。 –

+0

@DawidHyży試試這個 –

+0

由於缺乏時間,我不幸因爲使用EditText和ImageButton來模擬SearchView而結束了RelativeLayout。我會在下次嘗試。 –

0

我有同樣的問題,但對我來說,它發生,我在工具欄上有一個菜單抽屜圖標圖像(在佈局文件),並在同一時間已使home鍵編程,像這樣

getSupportActionBar().setDisplayHomeAsUpEnabled(true)

將此設置爲false(或刪除此代碼)爲抽屜圖標和SearchView釋放了空間。希望這可以幫助某人。