我有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>
截图:
修改
所以,现在我有这样的事情:
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 :(得分:1)
检测键盘是显示的还是可见的:
// 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
}
}
});
如果您的网页视图变形,请在键盘隐藏后使用此代码:
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;
}
}