dialog內部webview顯示網頁生成的二維碼(網頁非圖)寬度問題
阿新 • • 發佈:2019-01-01
使用webview外部佈局必須填滿或設定高度,所以佈局如下[二維碼原背景為無,chrome顯示為黑色,坑。。。]:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<WebView
android:id ="@+id/web"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
final View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.qrcode,null);
final android.webkit .WebView web = (android.webkit.WebView) view.findViewById(R.id.web);
web.getSettings().setUseWideViewPort(true);
web.getSettings().setLoadWithOverviewMode(true);
if(qrDialog == null){
qrDialog = new AlertDialog.Builder(MainActivity.this)
.setView(view).create();
}
getProgressDialog().show ();
final android.view.WindowManager.LayoutParams p = qrDialog.getWindow().getAttributes();
final ViewGroup.LayoutParams params = web.getLayoutParams();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}else
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
int height = view.getHeight();
params.width = PictureUtil.dip2px(MainActivity.this,height);
params.height = PictureUtil.dip2px(MainActivity.this,height);
p.width = PictureUtil.dip2px(MainActivity.this,height)+70;
qrDialog.getWindow().setAttributes(p);
web.setLayoutParams(params);
}
});
web.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
getProgressDialog().dismiss();
qrDialog.show();
}
});
web.loadUrl("二維碼網址。。。");
比較坑的是,還是做不到長寬一致只能保證二維碼顯示是完整的,所以這裡加了一個數字,看起來稍微自然。
p.width = PictureUtil.dip2px(MainActivity.this,height)+70;