2017-10-12 76 views
1

我有一个WebView,并在loadData负荷iframe中。当softkeyboard出现 - 在Webview变化的高度,但是当softkeyboard消失 - 在WebView的高度不会改变。如何改变WebView的高度回到全屏?的WebView(与iframe中)没有调整回来的时候softkeyboard消失

我的代码:

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"); 
+0

@NikolaLukic我想它是一个WebView,因为我为它设置了自定义的背景颜色并在变形之后BG为白色 –

+0

使用类似下面:YOUR_CONTROL.getLayoutParams()高度=介绍runOnUiThread确保也在HTML内容。 –

+0

重要提示:您需要使用FrameLayout(仅支持自定义位置 - 绝对)。 –

回答

1

检测键盘isHihhen或可见:

// 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; 
    } 



} 
+1

如果你还告诉我如何检测软键盘隐藏时,我将不胜感激。我试过不同的解决方案,但没有找到任何工作 –

+0

它使webview全屏,但不能调整大小。所以,现在我想在'onGlobalLayout()两次设置的LayoutParams'(当键盘打开时,我设置params1.height 45,当键盘被关闭,我设置params1.height至100),问题是的WebView不想将高度再次改为100。我试图用'webView.requestLayout();'webView.setLayoutParams(params1)之后','但它不”的帮助。我在帖子中更新了我的代码。 –

+0

不要忘记runOnUiThread! –