Проблема с растяжением экрана в приложении на Android
У меня есть приложение, в котором можно пролистывать экран вверх или вниз.
Когда я дохожу до верхней или нижней границы, в противоположном конце экрана происходит растяжение, и контент даже выходит за рамку экрана.
Подскажите, пожалуйста, как это отключить.
Также добавлю код layout
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="hdw.whaile.hdwnetweb.MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:gravity="center"
android:orientation="vertical"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ManiFest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:usesCleartextTraffic="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.HDWnetweb"
tools:targetApi="31">
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|screenLayout|smallestScreenSize"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustNothing"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MainActivity
import static androidx.core.content.ContextCompat.startActivity;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.UiModeManager;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends AppCompatActivity {
WebView webView;
public static boolean isDark = false;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.setWebViewClient(new WebClient());
webView.setWebChromeClient(new WebChromeClient());
WebSettings webset = webView.getSettings();
webset.setJavaScriptEnabled(true);
webset.setSaveFormData(false);
webset.setSavePassword(false);
webView.clearFormData();
determineTheme();
if (isDark) {
webView.loadUrl("file:///android_asset/loading_dark.html");
} else {
webView.loadUrl("file:///android_asset/loading.html");
}
new Handler().postDelayed(() -> {
webView.clearCache(true);
webView.clearHistory();
if (isDark) {
webView.loadUrl("https://hdwebnet.ru/phone/index?dark=true");
} else {
webView.loadUrl("https://hdwebnet.ru/phone/index");
}
}, 2000);
}
private class WebClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String link = "hdwebnet.ru";
if (url.startsWith("file:///") || url.startsWith("https://" + link) || url.startsWith("http://" + link)) {
return super.shouldOverrideUrlLoading(view, url);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
}
@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
if (isDark) {
view.loadUrl("file:///android_asset/error_page_dark.html");
} else {
view.loadUrl("file:///android_asset/error_page.html");
}
}
}
private void determineTheme() {
int currentNightMode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
isDark = false;
break;
case Configuration.UI_MODE_NIGHT_YES:
isDark = true;
break;
default:
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
UiModeManager uiModeManager = (UiModeManager) getSystemService(Context.UI_MODE_SERVICE);
if (uiModeManager != null && uiModeManager.getNightMode() == UiModeManager.MODE_NIGHT_YES) {
isDark = true;
}
}
}
}
И также скрины проблемы:
Ответы (1 шт):
Это может быть связано либо со стилями в вашем веб-контенте, либо со специфической реализацией WebView на конкретном устройстве или версии Android.
Это может быть связано со стилями веб-страницы, то вам стоит проверить CSS вашей веб-страницы, особенно свойства, относящиеся к прокрутке (overflow, height, max-height и т.д.), и убедиться, что они настроены таким образом, чтобы не было растяжения контента.

