我有一个简单的 Android 应用程序,其中包含相对布局和 webview:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<WebView
android:id="@+id/MyWebView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
webview中显示的页面是:
<html>
<body style="padding: 0; margin: 0; box-sizing: border-box; border: 5px solid red; width: 100vw; height: 100vh;">
test
<div style="width: 50vw; height: 70vh; border: 1px solid green;">some div</div>
<input placeholder="Type here and KB will cover this">
</body>
</html>
这就是我输入文本时的样子。当键盘出现时,Webview 会调整大小。一切都按预期进行。没问题。
现在我想让应用程序全屏显示。我像这样更改 styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:windowFullscreen">true</item>
</style>
该应用程序现已全屏显示。但是当我点击输入时,它被软键盘覆盖,我看不到我正在输入的内容。
我已经尝试了一切(android:windowSoftInputMode =“adjustResize”,调整Pan,不同的布局,在运行时更改为全屏, ScrollView )但没有任何效果。
最佳答案
- 调整大小
The activity's main window is always resized to make room for the soft keyboard on screen.
但是当您的应用程序处于全屏模式时它有局限性。 Android官方文档说:
If the window's layout parameter flags include FLAG_FULLSCREEN, this value for softInputMode will be ignored; the window will not resize, but will stay fullscreen.
查看更多信息:doc
Retrieve the overall visible display size in which the window this view is attached to has been positioned in. This takes into account screen decorations above the window, for both cases where the window itself is being position inside of them or the window is being placed under then and covered insets are used for the window to position its content inside. In effect, this tells you the available area where content can be placed and remain visible to users.
所以,我们需要检测键盘何时打开和隐藏,并根据 View 计算其大小。下面是示例代码。
示例代码:
list :
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
activity_main.xml:
<RelativeLayout 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:fitsSystemWindows="true"
android:id="@+id/frame"
android:layout_height="match_parent"
tools:context=".MainActivity">
<WebView
android:id="@+id/MyWebView1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
WebView webView;
RelativeLayout relativeLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_main);
relativeLayout=findViewById(R.id.frame);
webView=findViewById(R.id.MyWebView1);
this.getWindow().getDecorView().getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect r = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
int height = relativeLayout.getContext().getResources().getDisplayMetrics().heightPixels;
int diff = height - r.bottom;
if (diff != 0) {
if (relativeLayout.getPaddingBottom() != diff) {
relativeLayout.setPadding(0, 0, 0, diff);
}
} else {
if (relativeLayout.getPaddingBottom() != 0) {
relativeLayout.setPadding(0, 0, 0, 0);
}
}
}
});
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = "<html>\n" +
" <body style=\"padding: 0; margin: 0; box-sizing: border-box; border: 5px solid red; width: 100vw; height: 100vh;\">\n" +
" test\n" +
" <div style=\"width: 50vw; height: 70vh; border: 1px solid green;\">some div</div>\n" +
" <input placeholder=\"Type here and KB will cover this\">\n" +
" </body>\n" +
"</html>";
webView.loadDataWithBaseURL("", html, mimeType, encoding, "");
}
}
styles.xml:
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
输出屏幕:
关于Android 键盘覆盖 View ,但仅在全屏模式下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57621998/