您的位置:首页 > 移动开发

ScrollView中嵌套WebView时可能会出现焦点问题

2015-11-18 17:31 501 查看
  我们都知道,WebView 和 ScrollView都是可以滚动的,当这两个View嵌套时,容易出现一些问题。其中比较常见的,是嵌套在 ScrollView 中的WebView 的焦点问题.

  例如这个结构:

<ScrollView
android:id="@+id/sv"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="top" />
<WebView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</WebView>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bottom" />
</LinearLayout>
</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://stackoverflow.com/questions/9842494/how-to-prevent-a-scrollview-from-scrolling-to-a-webview-after-data-is-loaded

/article/5489065.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: