下面列出了android.webkit.WebView#setLayoutParams ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Dialog createDialog(Bundle savedInstanceState) {
String buffer = "<div align=\"center\"><h2><b>" + A.getAppName() + "</b></h2></div>" +
"<div align=\"center\"><h3><b>" + A.getAppVersion() + "</b></h3></div>" +
CustomMainActivity.loadAssetString(PreferenceValues.getLanguageCode() + "_first.html") +
"<br /><br />" +
CustomMainActivity.loadAssetString(PreferenceValues.getLanguageCode() + "_about.html") +
"<br /><br />" +
VersionInfo.getNews(1, PreferenceValues.getApplicationVersionActual());
// add info
// add about info
// add news
WebView webView = new WebView(A.getMain());
webView.loadData(buffer, "text/html; charset=utf-8", "utf-8");
webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
webView.setBackgroundColor(Color.WHITE);
return new AlertDialog.Builder(getActivity()).setTitle(R.string.about_application)
.setIcon(R.drawable.ic_title_logo).setView(webView).setNeutralButton(R.string.close, null)
.create();
}
public static WebView getFilledWebView(Activity activity, String data) {
WebView webView = new WebView(activity);
// webView.loadData(UtilsHttp.repairHtmlFile(data), "text/html", "utf-8");
try {
// webView.loadData(URLEncoder.encode(
// UtilsHttp.repairHtmlFile(data),"utf-8").replaceAll("\\+"," "),
// "text/html", "utf-8");
// http://stackoverflow.com/questions/4917860/android-character-encoding-raw-resource-files
webView.loadDataWithBaseURL(null, data.replaceAll("\\+", " "), "text/html", "utf-8", null);
} catch (Exception e) {
}
webView
.setLayoutParams(new ViewGroup.LayoutParams(getDialogWidth(), LayoutParams.WRAP_CONTENT));
webView.setBackgroundColor(Color.WHITE);
return webView;
}
@SuppressLint("SetJavaScriptEnabled")
private void setUpWebView(int margin) {
LinearLayout webViewContainer = new LinearLayout(getContext());
webView = new WebView(getContext());
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.setWebViewClient(new DialogWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
webView.setVisibility(View.INVISIBLE);
webView.getSettings().setSavePassword(false);
webViewContainer.setPadding(margin, margin, margin, margin);
webViewContainer.addView(webView);
contentFrameLayout.addView(webViewContainer);
}
@Override
public View getView() {
FrameLayout root = new FrameLayout(mContext);
root.setBackgroundColor(Color.WHITE);
mWebView = new WebView(mContext);//mContext.getApplicationContext();
FrameLayout.LayoutParams wvLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
wvLayoutParams.gravity = Gravity.CENTER;
mWebView.setLayoutParams(wvLayoutParams);
root.addView(mWebView);
initWebView(mWebView);
mProgressBar = new ProgressBar(mContext);
showProgressBar(false);
FrameLayout.LayoutParams pLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
mProgressBar.setLayoutParams(pLayoutParams);
pLayoutParams.gravity = Gravity.CENTER;
root.addView(mProgressBar);
return root;
}
private void initWebView(){
mWebView = new WebView(mContext);
ConstraintLayout.LayoutParams layoutParams =
new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
layoutParams.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.PARENT_ID;
mWebView.setLayoutParams(layoutParams);
mWebView.setId(R.id.webview);
mWebView.setVisibility(View.INVISIBLE);
mLayout.addView(mWebView,-1);
mAdvanceWebClient = new AdvanceWebClient(getContext(), mWebView,mUser_agent);
mAdvanceWebClient.setListener(mLoginSuccessListener);
mAdvanceWebClient.initWebView("https://" + mUrl.getHost());
}
private void setUpWebView() {
mWebView = new WebView(getContext());
mWebView.setVerticalScrollBarEnabled(false);
mWebView.setHorizontalScrollBarEnabled(false);
mWebView.setWebViewClient(new InstagramWebViewClient());
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl(mAuthUrl);
mWebView.setLayoutParams(FILL);
WebSettings webSettings = mWebView.getSettings();
webSettings.setSavePassword(false);
webSettings.setSaveFormData(false);
mContent.addView(mWebView);
}
@SuppressLint("SetJavaScriptEnabled")
private void setUpWebView(int margin) {
LinearLayout webViewContainer = new LinearLayout(getContext());
webView = new WebView(getContext());
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.setWebViewClient(new DialogWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
webView.setVisibility(View.INVISIBLE);
webView.getSettings().setSavePassword(false);
webViewContainer.setPadding(margin, margin, margin, margin);
webViewContainer.addView(webView);
webViewContainer.setBackgroundColor(BACKGROUND_GRAY);
contentFrameLayout.addView(webViewContainer);
}
@Override
public View getView() {
FrameLayout root = new FrameLayout(mContext);
root.setBackgroundColor(Color.WHITE);
mWebView = new WebView(mContext);//mContext.getApplicationContext();
FrameLayout.LayoutParams wvLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
wvLayoutParams.gravity = Gravity.CENTER;
mWebView.setLayoutParams(wvLayoutParams);
root.addView(mWebView);
initWebView(mWebView);
mProgressBar = new ProgressBar(mContext);
showProgressBar(false);
FrameLayout.LayoutParams pLayoutParams =
new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
mProgressBar.setLayoutParams(pLayoutParams);
pLayoutParams.gravity = Gravity.CENTER;
root.addView(mProgressBar);
return root;
}
@SuppressLint("SetJavaScriptEnabled")
private void setUpWebView(int margin) {
Log8.d();
LinearLayout webViewContainer = new LinearLayout(getContext());
webView = new WebView(getContext());
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.setWebViewClient(new DialogWebViewClient());
//webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
webView.setVisibility(View.INVISIBLE);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSaveFormData(false);
webViewContainer.setPadding(margin, margin, margin, margin);
webViewContainer.addView(webView);
webViewContainer.setBackgroundColor(0xCC000000);
contentFrameLayout.addView(webViewContainer);
Log8.d();
}
@SuppressLint("SetJavaScriptEnabled")
private void setUpWebView(int margin) {
LinearLayout webViewContainer = new LinearLayout(getContext());
webView = new WebView(getContext());
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.setWebViewClient(new DialogWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
webView.setVisibility(View.INVISIBLE);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSaveFormData(false);
webViewContainer.setPadding(margin, margin, margin, margin);
webViewContainer.addView(webView);
webViewContainer.setBackgroundColor(BACKGROUND_GRAY);
contentFrameLayout.addView(webViewContainer);
}
@SuppressLint("SetJavaScriptEnabled")
private void setUpWebView(int margin) {
LinearLayout webViewContainer = new LinearLayout(getContext());
webView = new WebView(getContext());
webView.setVerticalScrollBarEnabled(false);
webView.setHorizontalScrollBarEnabled(false);
webView.setWebViewClient(new DialogWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
webView.setVisibility(View.INVISIBLE);
webView.getSettings().setSavePassword(false);
webView.getSettings().setSaveFormData(false);
webViewContainer.setPadding(margin, margin, margin, margin);
webViewContainer.addView(webView);
webViewContainer.setBackgroundColor(BACKGROUND_GRAY);
contentFrameLayout.addView(webViewContainer);
}
private View makeWebView(int index) {
final String url = getResources().getStringArray(R.array.server_websites)[index];
final WebView webView = new WebView(getContext());
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient()); // Makes navigation stay in this webview.
webView.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
webView.loadUrl(url);
return webView;
}
@Override
View createView() {
if (getContext() == null)
return new View(getContext());
LayoutInflater inflater = LayoutInflater.from(getContext());
webView = new WebView(inflater.getContext());
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
webView.setBackgroundColor(AppTheme.getThemeStyleWebViewBackground());
loadWebView();
return webView;
}
private void c()
{
l = new ProgressBar((Context)c.get());
android.widget.FrameLayout.LayoutParams layoutparams = new android.widget.FrameLayout.LayoutParams(-2, -2);
layoutparams.gravity = 17;
l.setLayoutParams(layoutparams);
(new TextView((Context)c.get())).setText("test");
k = new FrameLayout((Context)c.get());
android.widget.FrameLayout.LayoutParams layoutparams1 = new android.widget.FrameLayout.LayoutParams(-1, -2);
layoutparams1.bottomMargin = 40;
layoutparams1.leftMargin = 80;
layoutparams1.rightMargin = 80;
layoutparams1.topMargin = 40;
layoutparams1.gravity = 17;
k.setLayoutParams(layoutparams1);
k.setBackgroundResource(0x1080000);
k.addView(l);
android.widget.FrameLayout.LayoutParams layoutparams2 = new android.widget.FrameLayout.LayoutParams(-1, -1);
j = new WebView((Context)c.get());
j.setLayoutParams(layoutparams2);
i = new FrameLayout((Context)c.get());
layoutparams2.gravity = 17;
i.setLayoutParams(layoutparams2);
i.addView(j);
i.addView(k);
d = new WeakReference(k);
setContentView(i);
}
@Override
View createView() {
LayoutInflater inflater = LayoutInflater.from(getContext());
webView = new WebView(inflater.getContext());
webView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
webView.setBackgroundColor(AppTheme.getThemeStyleWebViewBackground());
loadWebView();
return webView;
}
private void c()
{
m = new a((Context)o.get());
m.setBackgroundColor(0x66000000);
m.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(-1, -1));
n = new WebView((Context)o.get());
n.setBackgroundColor(0);
n.setBackgroundDrawable(null);
android.widget.RelativeLayout.LayoutParams layoutparams;
if (android.os.Build.VERSION.SDK_INT >= 11)
{
try
{
Class aclass[] = new Class[2];
aclass[0] = Integer.TYPE;
aclass[1] = android/graphics/Paint;
Method method = android/view/View.getMethod("setLayerType", aclass);
WebView webview = n;
Object aobj[] = new Object[2];
aobj[0] = Integer.valueOf(1);
aobj[1] = new Paint();
method.invoke(webview, aobj);
}
catch (Exception exception)
{
exception.printStackTrace();
}
}
layoutparams = new android.widget.RelativeLayout.LayoutParams(-1, p);
layoutparams.addRule(13, -1);
n.setLayoutParams(layoutparams);
m.addView(n);
m.a(this);
setContentView(m);
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//устанавливаем кастомный бэкграунд акшенбара
getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_bgr));
//добавляем кнопку назад
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
activity = this;
aq = new AQuery(activity);
Bundle extras = getIntent().getExtras();
if (extras == null) {
return;
} else {
q = extras.getString("q");
}
//UI
final LinearLayout linearLayout = new LinearLayout(activity);
linearLayout.setOrientation(LinearLayout.VERTICAL);
//Progress
progressBar = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
progressBar.setLayoutParams(layoutParams);
progressBar.setVisibility(View.GONE);
webView = new WebView(activity);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().contains("freemp.org")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
);
ViewGroup.LayoutParams layoutParams2 = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
webView.setLayoutParams(layoutParams2);
linearLayout.addView(progressBar);
linearLayout.addView(webView);
setContentView(linearLayout);
}
@SuppressLint("JavascriptInterface")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
mSmoothProgress = findViewById(R.id.smooth_progress);
Bundle bundle = getIntent().getBundleExtra("bundle");
mUrl = bundle.getString("url");
mTitle = bundle.getString("title", "");
mJavaScriptInterface = bundle.getSerializable("javascript");
initBackToolbar(TextUtils.isEmpty(mTitle) ? "加载中" : mTitle);
Log.d("Url:", mUrl);
Log.d("mTitle:", mTitle);
mLayout = (LinearLayout) findViewById(R.id.web_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mWebView = new WebView(getApplicationContext());
mWebView.setLayoutParams(params);
mLayout.addView(mWebView);
WebSettings mWebSettings = mWebView.getSettings();
mWebSettings.setSupportZoom(true);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setDefaultTextEncodingName("utf-8");
mWebSettings.setLoadsImagesAutomatically(true);
//调用JS方法.安卓版本大于17,加上注解 @JavascriptInterface
mWebSettings.setJavaScriptEnabled(true);
if (mJavaScriptInterface != null) {
mWebView.addJavascriptInterface(mJavaScriptInterface, "android");
}
mWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
downloadByBrowser(url);
}
});
saveData(mWebSettings);
newWin(mWebSettings);
mWebView.setWebChromeClient(webChromeClient);
mWebView.setWebViewClient(webViewClient);
mWebView.loadUrl(mUrl);
}
@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());
}
});
}
/**
* 1、不在xml中定义Webview,而是在需要的时候在Activity中创建
* 使用getApplicationgContext(),避免内存泄漏。
* <p>
* 2、当然,你也可以配置webView所在Activity,
* 在AndroidManifest中的进程为:android:process=":remote"
* 避免泄漏影响主进程
**/
void addWebViewToLayout() {
mRootLayout = (FrameLayout) findViewById(R.id.js_root_layout);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mWebView = new WebView(getApplicationContext());
mWebView.setLayoutParams(params);
mRootLayout.addView(mWebView);
}