2017-10-12 31 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! –