【发布时间】:2020-10-29 18:32:02
【问题描述】:
我在https://bugs.chromium.org/p/chromium/issues/detail?id=1144713打开了一个错误报告
当我旋转 webview 时,它不会填满屏幕。
我想旋转 web 视图,因为 AndroidTV 需要横向模式...但我们要让电视处于纵向设置。
我使用fill_parent && match_parent,但在这两种情况下都不起作用。
这是 Android Studio 预览的屏幕截图。
还要注意,当我像这样测试它时,webview 似乎与屏幕尺寸匹配但不适应旋转...它只是旋转 webview 而不会将其调整为新尺寸
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:background="@drawable/game_bg"
android:orientation="vertical"
tools:context="com.surveyswithgames.app.wheel.KeypadActivity">
<FrameLayout
android:id="@+id/frameParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:layout_marginStart="2dp"
android:layout_marginTop="2dp"
android:background="@android:color/transparent"
android:foregroundGravity="center"
android:keepScreenOn="true">
<WebView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keypadWebView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:rotation="-90"
/>
</FrameLayout>
</RelativeLayout>
更新: 根据https://stackoverflow.com/a/64672893/1815624 的答案,我使用了自定义 WebView 类,但似乎 setMeasuredDimension 不起作用...
public class CustomWebView extends WebView {
public CustomWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomWebView(Context context) {
super(context);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int desiredWidth = MeasureSpec.getSize(heightMeasureSpec);//getMeasuredHeight();
int desiredHeight = MeasureSpec.getSize(widthMeasureSpec);//getMeasuredWidth();
Log.e("CustomWebView", "desiredWidth: "+ desiredWidth);
Log.e("CustomWebView", "desiredHeight: "+ desiredHeight);
setMeasuredDimension(desiredWidth, desiredHeight);
}
}
更新 2: 我不得不使用答案https://stackoverflow.com/a/64672893/1815624 建议的代码和布局,然后它起作用了......干杯
【问题讨论】:
-
您缺少翻译。
-
我按照建议颠倒了它们,即使我首先在代码中做了(desiredWidth = hieght),但我也尝试手动设置值以及像这样
setMeasuredDimension(100, 200)并且仍然没有改变尺寸...还有其他建议吗? -
翻译?我不明白没关系,我看到你更新了答案
标签: android webview android-xml