我們都知道,WebView 和 ScrollView都是可以滾動的,當這兩個View嵌套時,容易出現一些問題。其中比較常見的,是嵌套在 ScrollView 中的WebView 的焦點問題.
例如這個結構:
1 <ScrollView 2 android:id="@+id/sv" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent"> 5 <LinearLayout 6 android:layout_width="fill_parent" 7 android:layout_height="wrap_content" 8 android:orientation="vertical"> 9 <TextView 10 android:layout_width="fill_parent" 11 android:layout_height="wrap_content" 12 android:text="top" /> 13 <WebView 14 android:id="@+id/lv" 15 android:layout_width="fill_parent" 16 android:layout_height="wrap_content"> 17 </WebView> 18 <TextView 19 android:layout_width="fill_parent" 20 android:layout_height="wrap_content" 21 android:text="bottom" /> 22 </LinearLayout> 23 </ScrollView>
如果不做任何處理,則會出現WebView加載后獲得焦點,在WebView並非占滿屏幕時點擊WebView內容,ScrollView就會自動滾動,使WebView占滿屏幕。(ListView也會出現類似問題,即使修正了高度,也會主動獲得焦點,使得屏幕產生錯誤的滾動)。經過一番搜索研究,發現解決方法竟出奇的簡單,只需要一行:
<LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:descendantFocusability="blocksDescendants">
descendantFocusability屬性的作用是當一個view獲取焦點時,定義viewGroup和其子控件兩者之間的關系。而blocksDescendants是viewgroup會覆蓋子類控件而直接獲得焦點。
參考:
http://www.cnblogs.com/eyu8874521/archive/2012/10/17/2727882.html