遇到ScrollView中嵌入WebView,针对部分手机第一次加载WebView出现白屏(即WebView无法展示)。解决方法是去掉WebView的滚动效果,让它按照既有的长度展示,由外层ScrollView来滚动展示WebView。
public class ScrollWebView extends WebView {
public ScrollWebView(Context context) {
this(context, null);
}
public ScrollWebView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ScrollWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
WebSettings webSettings = getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setDomStorageEnabled(true);
webSettings.setDefaultTextEncodingName("UTF-8");
webSettings.setAllowUniversalAccessFromFileURLs(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, mExpandSpec);
}
}
网友评论