ScrollView中嵌套WebView时可能会出现焦点问题
2015-11-18 17:31
501 查看
我们都知道,WebView 和 ScrollView都是可以滚动的,当这两个View嵌套时,容易出现一些问题。其中比较常见的,是嵌套在 ScrollView 中的WebView 的焦点问题.
例如这个结构:
如果不做任何处理,则会出现WebView加载后获得焦点,在WebView并非占满屏幕时点击WebView内容,ScrollView就会自动滚动,使WebView占满屏幕。(ListView也会出现类似问题,即使修正了高度,也会主动获得焦点,使得屏幕产生错误的滚动)。经过一番搜索研究,发现解决方法竟出奇的简单,只需要一行:
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
例如这个结构:
<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
相关文章推荐
- android gridview画分割线
- Android过渡动画之共享元素实现以及遇到的一些问题
- Android各种效果集合
- ios实现热更新(无需发新版本实现app添加新功能)
- Android 去标题栏,Activity 满屏那些你所不知道的 Theme Style
- Android 检查内存溢出
- 解决:AndroidStudio导入第三方工程出现Error:Attribute application@icon value=(@mipmap/ic_launcher)
- [Android] EditText 的 inputType属性
- Universal-Image-Loader 使用
- Fragment碎片应用--新闻APP
- js中call、apply、bind的用法
- 获取当前安装app列表
- 一路走来 Android NDK 踩过的坑
- iOS9项目打包上线
- android中常见的Drawable资源有哪些?
- AndroidAnnotations简单示例
- Android Context完全解析,你所不知道的Context的各种细节
- android中之断点续传
- Android自定义手势识别
- 【Android学习笔记】Broadcast receiver