下面列出了android.webkit.WebView#getLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = inflater.getContext();
DisplayMetrics metrics = context.getResources().getDisplayMetrics();
final int DIALOG_HEIGHT =
(int) Math.min(0.8f * metrics.heightPixels, 1024);
View root = inflater.inflate(R.layout.custom_ui, container, false);
WebView wv = (WebView) root.findViewById(android.R.id.primary);
LayoutParams params = wv.getLayoutParams();
params.height = DIALOG_HEIGHT;
wv.setLayoutParams(params);
View progressContainer = root.findViewById(android.R.id.progress);
params = progressContainer.getLayoutParams();
params.height = DIALOG_HEIGHT;
progressContainer.setLayoutParams(params);
return root;
}
public WebViewCache(Context context, String url, int size) {
mWebViewList = new ArrayList<>(size);
int width = ScreenAdapterUtils.getScreenWidth();
int height = ScreenAdapterUtils.getAdHeight();
for (int i = 0; i < DEFAULT_WEBVIEW_NUM; i++){
WebView webView = new WebView(context);
webView.loadUrl(url);
ViewGroup.LayoutParams layoutParams = webView.getLayoutParams();
if (layoutParams == null){
layoutParams = new ViewGroup.LayoutParams(width, height);
} else {
layoutParams.width = width;
layoutParams.height = height;
}
webView.setLayoutParams(layoutParams);
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
WebSettings settings = webView.getSettings();
settings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new TyJavaScriptInterface(context), "android");
mWebViewList.add(webView);
}
}
private void init(Context context) {
mWebView = new WebView(context);
ViewGroup.LayoutParams params = mWebView.getLayoutParams();
if (params == null) {
params = new ViewGroup.LayoutParams(-1, -1);
}
params.width = -1;
params.height = -1;
addView(mWebView, params);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flexiblespacetoolbarwebview);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
ActionBar ab = getSupportActionBar();
if (ab != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mFlexibleSpaceView = findViewById(R.id.flexible_space);
mTitleView = (TextView) findViewById(R.id.title);
mTitleView.setText(getTitle());
setTitle(null);
mToolbarView = findViewById(R.id.toolbar);
mWebViewContainer = findViewById(R.id.webViewContainer);
final ObservableScrollView scrollView = (ObservableScrollView) findViewById(R.id.scroll);
scrollView.setScrollViewCallbacks(this);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadUrl("file:///android_asset/lipsum.html");
mFlexibleSpaceHeight = getResources().getDimensionPixelSize(R.dimen.flexible_space_height);
int flexibleSpaceAndToolbarHeight = mFlexibleSpaceHeight + getActionBarSize();
final FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) webView.getLayoutParams();
layoutParams.topMargin = flexibleSpaceAndToolbarHeight;
webView.setLayoutParams(layoutParams);
mFlexibleSpaceView.getLayoutParams().height = flexibleSpaceAndToolbarHeight;
ScrollUtils.addOnGlobalLayoutListener(mTitleView, new Runnable() {
@Override
public void run() {
updateFlexibleSpaceText(scrollView.getCurrentScrollY());
}
});
}