2016-11-17 62 views
2

我注意到,如果我將android.support.v7.widget.SearchView放置在android.support.v7.widget.Toolbar的內部,其高度設置爲wrap_contentSearchView的行爲非常奇怪。它看起來很好,但是一旦獲得焦點,它就會消失,不再佔用佈局空間。這可以通過設置Toolbar的明確高度來解決,但我試圖使用wrap_content,因爲我的用例需要Toolbar動態調整大小。如何將SearchView放置在ToolBar中,高度設置爲wrap_content而不會消失?

也可能值得一提的是SearchView在消失後仍然有效。如果我將SearchView.OnQueryTextListener添加到SearchView那麼我可以觀察到查詢文本確實在我輸入時正在更新。雖然此文本或SearchView的任何其他部分都不佔用佈局中的空間,但如果您在佈局中強制使其無法被遮擋,那麼它仍然不可見。

這種看起來像SearchView給我的錯誤。有人有更深入的瞭解爲什麼發生或如何解決它?

這裏的問題的工作示例:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.me.MyActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/appbar_padding_top" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      app:layout_scrollFlags="scroll|enterAlways" 
      app:popupTheme="@style/AppTheme.PopupOverlay"> 

      <android.support.v7.widget.SearchView 
       android:id="@+id/internalSearchView" 
       app:queryHint="To..." 
       android:layout_weight="1" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" > 
      </android.support.v7.widget.SearchView> 

     </android.support.v7.widget.Toolbar> 

    </android.support.design.widget.AppBarLayout> 

</android.support.design.widget.CoordinatorLayout> 
+0

Ÿ着u盤比賽作爲家長的高度,它將匹配的工具欄高度 – Ak9637

+0

這將很好地工作在這個簡單的例子,但我希望它是'wrap_content',因爲我確實有額外的動態視圖在那裏,更改有效高度並需要「工具欄」展開。在沒有額外視圖的情況下可以觀察到越野車行爲,所以我將它們從示例代碼中移除。 –

回答

1

這肯定看起來像它應該工作...我有兩個建議。

  1. 如果它不影響您的設計目標,請在工具欄上設置android:minHeight="?attr/actionBarSize"。無論如何,這可能是需要的。
  2. 嘗試在SearchView上設置android:layout_height="48dp"
+0

添加'minHeight'沒有效果,但指定'layout_height'顯式工作!感謝您的解決方法! –

+0

不錯!真高興你做到了。 – Mike

相關問題