上图

首先是Main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity">
<TextView android:id="@+id/text" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:padding="5dp" android:text="入住指南" android:layout_width="wrap_content" android:layout_height="wrap_content" />
</RelativeLayout>
之后是diglog_item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <WebView android:id="@+id/web_view" android:layout_width="match_parent" android:layout_height="@dimen/dp_400"/> <LinearLayout android:layout_marginTop="3dp" android:layout_below="@id/web_view" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/web_close" android:layout_weight="1" android:text="关闭" android:layout_below="@id/web_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/web_ok" android:layout_weight="1" android:text="确定" android:layout_below="@id/web_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
在之后就是Main.java
package com.example.myapplication;
import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mText; private String loadUrl = "https://www.iycgs.com/index.php?m=Api&c=Newjoin&a=guidance"; private WebView web_view;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mText = findViewById(R.id.text); mText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { createDialog(); } }); }
private void createDialog() { Builder builder = new AlertDialog.Builder(this); final AlertDialog alertDialog = builder.create(); final View dialogView = View.inflate(this, R.layout.diglog_item, null); alertDialog.setView(dialogView); web_view = dialogView.findViewById(R.id.web_view); //webView WebSettings webSettings = web_view.getSettings(); //支持js,如果不设置本行,html中的js代码都会失效 webSettings.setJavaScriptEnabled(true); //提高渲染的优先级 webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); //将图片调整到适合webview的大小 webSettings.setUseWideViewPort(true); //缩放至屏幕大小 webSettings.setLoadWithOverviewMode(true); //支持自动加载图片 webSettings.setLoadsImagesAutomatically(true); //设置编码格式 webSettings.setDefaultTextEncodingName("utf-8"); //支持内容重新布局 webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); web_view.addJavascriptInterface(this, "android"); webSettings.setUseWideViewPort(true); float maxsp = getResources().getDimension(R.dimen.sp_12); final int t = (int) maxsp; webSettings.setDefaultFontSize(t); Button web_close = dialogView.findViewById(R.id.web_close); Button web_ok = dialogView.findViewById(R.id.web_ok); web_view.loadUrl(loadUrl); web_close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alertDialog.dismiss(); } }); web_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { alertDialog.dismiss(); } }); alertDialog.show(); }
}
|