WebView中的怪异文本缩放问题
我的游戏项目有问题。该游戏基于用于加载 url 的网络浏览量如果您更改设置中的字体大小,请遇到文本缩放问题,,如下所示。
我无法确定问题的来源,但是确实很烦人,尤其是在使用更大的字体尺寸的情况下。
这是XML文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"/>
</RelativeLayout>
这是活动文件:
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomWebViewClient client = new CustomWebViewClient(this);
webView = findViewById(R.id.webView);
webView.setWebViewClient(client);
webView.setBackgroundColor(0xfaf8ef);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.clearCache(true);
webView.loadUrl("http://rodoapps.ihostfull.com/gametest/");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK && this.webView.canGoBack()){
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
class CustomWebViewClient extends WebViewClient{
private Activity activity;
public CustomWebViewClient(Activity activity){
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request){
return false;
}
}
对不起我的英语,我不是本地人。
I have a problem in my game project. The game is based on a webview used to load an url where the game is present, but it seems to have text scaling issues if you change the font size in settings, as shown here.
I can't determine where the problem is coming from, but it gets really annoying, especially if even bigger font sizes are used.
Here's the xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"/>
</RelativeLayout>
And here is activity file:
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class MainActivity extends AppCompatActivity {
private WebView webView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomWebViewClient client = new CustomWebViewClient(this);
webView = findViewById(R.id.webView);
webView.setWebViewClient(client);
webView.setBackgroundColor(0xfaf8ef);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setAppCacheEnabled(false);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.clearCache(true);
webView.loadUrl("http://rodoapps.ihostfull.com/gametest/");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode == KeyEvent.KEYCODE_BACK && this.webView.canGoBack()){
this.webView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
class CustomWebViewClient extends WebViewClient{
private Activity activity;
public CustomWebViewClient(Activity activity){
this.activity = activity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest request){
return false;
}
}
Sorry for my English, I am not a native.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
灵感来自这个答案,我设法解决了我的问题。
与该答案不同的唯一方法是,我的用例需要删除一行代码。
这是我的解决方案:
Inspired from this answer, I have managed to solve my problem.
The only thing different from that answer is that my use case required removing one line of code.
Here's my solution: