android - 当软键盘消失时,WebView(内部带有 iframe)不会调整大小

标签 android iframe webview

我有一个 WebView,并在 loadData 中加载 iframe。当软键盘出现时 - Webview 改变高度,但当软键盘消失时 - WebView 的高度不变。如何将 WebView 的高度更改回全屏?

我的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView webView = (WebView) findViewById(R.id.wbv);
    webView.getSettings().setJavaScriptEnabled(true);
    String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width="100%" height="100%"></iframe></body></html>";
    webView.loadData(html,"text/html","UTF-8");

}

xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="app.hr_bot.MainActivity">

    <WebView
        android:id="@+id/wbv"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginBottom="0dp"
        android:layout_marginLeft="0dp"
        android:layout_marginRight="0dp"
        android:layout_marginTop="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

截图:

enter image description here enter image description here

编辑:

所以,现在我有这样的东西:

final EKRAN myEkran = new EKRAN(MainActivity.this);
        MAIN  =   findViewById(R.id.rootLayout);

        MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                Rect r = new Rect();
                MAIN.getWindowVisibleDisplayFrame(r);
                int screenHeight = MAIN.getRootView().getHeight();
                int keypadHeight = screenHeight - r.bottom;

                Log.d( String.valueOf(1) , "keypadHeight = " + keypadHeight);

                if (keypadHeight > screenHeight * 0.10) { // 0.15 ratio is perhaps enough to determine keypad height.
                    // keyboard is opened
                    Toast.makeText( MainActivity.this , "COOL1 " , Toast.LENGTH_SHORT).show();
                    ViewGroup.LayoutParams params1 = webView.getLayoutParams();
                    params1.width = myEkran.W(100); // IN %
                    params1.height = myEkran.H(45); // IN %
                    webView.setLayoutParams(params1);
                    webView.requestLayout();
                }
                else {
                    Toast.makeText( MainActivity.this , "COOL2 " , Toast.LENGTH_SHORT).show();
                    // keyboard is closed

                    ViewGroup.LayoutParams params1 = webView.getLayoutParams();
                    params1.width = myEkran.W(100); // IN %
                    params1.height = myEkran.H(92); // IN %
                    webView.setLayoutParams(params1);
                    webView.requestLayout();
                }
            }
        });


         webView = (WebView) findViewById(R.id.wbv);
        webView.getSettings().setJavaScriptEnabled(true);
        String html = "<html><body><iframe src=\"https://api.motion.ai/webchat/85714?color=EFB426&sendBtn=%D0%9E%D1%82%D0%B2%D0%B5%D1%82%D0%B8%D1%82%D1%8C&inputBox=%D0%9D%D0%B0%D0%BF%D0%B8%D1%88%D0%B8%D1%82%D0%B5%20%D0%B2%D0%B0%D1%88%20%D0%B2%D0%BE%D0%BF%D1%80%D0%BE%D1%81%2F%D0%BE%D1%82%D0%B2%D0%B5%D1%82&token=9b32ca23536d9816b448d2e4c53434dc\" width=\"100%\" height=\"100%\"></iframe></body></html>";
        webView.loadData(html,"text/html","UTF-8");

最佳答案

检测键盘是高亮还是可见:

// MAIN is root view see in design window (also enter id for root view ):

View MAIN;

...

    //########################
    // Put this intro OnCreate 
    //########################

    MAIN =   findViewById(R.id.main);

    MAIN.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            Rect r = new Rect();
            MAIN.getWindowVisibleDisplayFrame(r);
            int screenHeight = MAIN.getRootView().getHeight();
            int keypadHeight = screenHeight - r.bottom;

            Log.d( String.valueOf(1) , "keypadHeight = " + keypadHeight);

            if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
                // keyboard is opened
                Toast.makeText( context_pass , "COOL1 " , 1);
            }
            else {
                Toast.makeText( context_pass , "COOL2 " , 1);
                // keyboard is closed
            }
        }
    });

如果您的 webview 在键盘隐藏后变形,请使用此代码:

 ViewGroup.LayoutParams params1 = YOUR_CONTROL.getLayoutParams();
                    params1.width = MY_EKRAN.W(100); // IN %
                    params1.height = MY_EKRAN.H(100); // IN %
                    YOUR_CONTROL.setLayoutParams(params1);


 OR


  runOnUiThread(new Runnable() {
            public void run() {

              // runs on UI thread
              YOUR_CONTROL.getLayoutParams().height = ( MY_EKRAN.HEIGHT()/100*10);

                }
            });

DEF :

//global
  EKRAN MY_EKRAN;

 // in onCreate
  MY_EKRAN = new EKRAN( context_pass );

直径尺寸和位置的类(添加新文件 - 新 java 类):

import android.content.Context;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.util.TypedValue;
/**
 * Created by nikola on 10/17/16.
 */
//##############################################
// SCREEN - EKRAN CLASS
//##############################################
public class EKRAN {

    DisplayMetrics dm = new DisplayMetrics();
    Point size_ = new Point();
    static int width;
    static int height;

    EKRAN(Context CONTEXT_) {

        dm = CONTEXT_.getResources().getDisplayMetrics();
        int densityDpi = dm.densityDpi;
        height = dm.heightPixels;
        width = dm.widthPixels;

    }

    public static int WIDTH() {

        return width;

    }
    public static int HEIGHT(){

        return height;

    }
    public int W( int PER_ ){

        return width/100*PER_;

    }
    public int H( int PER_   ){


        return height/100*PER_;

    }

    //////////////////
    //extras
    /////////////////
    public int GET_PIX_FROM_DP ( float DP_VALUE )
    {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, DP_VALUE , dm );
    }

    public int GET_PIX_FROM_DP2 ( float DP_VALUE )
    {
        float res = DP_VALUE * ( dm.ydpi / 160f);
        return  (int) res;
    }



}

关于android - 当软键盘消失时,WebView(内部带有 iframe)不会调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46702887/

相关文章:

android - WebView 抛出 Receiver not registered : android. widget.ZoomButtonsController

Android 工具栏不从 Backstack 上的 fragment 调用 onOptionsItemSelected

android - 是否有 testflight for android 的替代品

java - Android 中是否有类似于 C/C++ 中的 "int main"的函数,其中包含程序的主循环?

iframe - 打开 iframe Colorbox 时出现白色闪烁

cocoa - Web View 更改字体: and changeColor: with no selection reset changes when i type

java - Android:如何在 webview 完成加载 url 之前制作启动画面?

java - 如何改变TextView的文字

html - 如何使 iframe 安全?

javascript - 从 IFRAME 获取当前 URL