我正在创建一个Webview与其他元素内联的应用程序,嵌套在ScrollView中。我注意到在ICS中,在Galaxy Nexus设备上进行测试时,当页面被抛出时,WebView似乎与其余显示的元素脱节,导致WebView看起来漂浮,因为它有几毫秒的价值。落后于绘画。这不会发生在Android的2.x版本中(未在3.x上测试)。这是一个浮动效果http://www.youtube.com/watch?v=avfBoWmooM4的视频(如果你设置为1080p全屏,你可以看清楚)
任何人都可以建议为什么会发生这种情况或修复?
我在下面设置了一个测试项目来演示:
package com.martynhaigh.floating_web_view;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class FloatingWebViewTestActivity extends FragmentActivity {
public final int viewId = 0x12345678;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
FrameLayout frame = new FrameLayout(this);
frame.setId(viewId);
setContentView(frame, lp);
FloatingWebViewICSFragment fragment = new FloatingWebViewICSFragment();
getSupportFragmentManager().beginTransaction().add(viewId, fragment).commit();
}
public static class FloatingWebViewICSFragment extends Fragment {
private final String htmlBody = "<html><body><p style=\"font-size:1.5em\">This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. This is a test document. </body></html>";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity().getApplicationContext());
textView.setText("Test Activity");
WebView webView = new WebView(getActivity().getApplicationContext());
webView.loadDataWithBaseURL("file:///android_asset/", htmlBody, "text/html", "UTF-8", "about:blank");
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webView.setScrollContainer(false);
TextView textView2 = new TextView(getActivity().getApplicationContext());
textView2.setText("Test Activity");
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
LinearLayout layout = new LinearLayout(getActivity().getApplicationContext());
layout.setLayoutParams(lp);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(textView);
layout.addView(webView);
layout.addView(textView2);
ScrollView scrollView = new ScrollView(getActivity().getApplicationContext());
scrollView.setLayoutParams(lp);
scrollView.addView(layout);
return scrollView;
}
}
}
答案 0 :(得分:1)
我不知道如何解决它,但我现在的原因。 WebView的内容由android.webkit.WebViewCore在其单独的工作线程中呈现。他们正在相互沟通。当WebView需要重新渲染时,它会向WebViewCore发送“please render”消息,当WVC准备就绪时,然后发送回结果。关键是它们的渲染与其他UI元素的渲染不同步 - 因为它是在一个单独的非ui线程上完成的。
我猜他们想避免在所有渲染工作中阻止ui-thread。那很亲切。但它会导致其他问题......就像你的一样。