gpt4 book ai didi

android - 如何用旋转的 webview 填充屏幕?

转载 作者:行者123 更新时间:2023-12-04 08:40:19 25 4
gpt4 key购买 nike

我在 https://bugs.chromium.org/p/chromium/issues/detail?id=1144713 打开了一个错误报告
当我旋转 webview 时,它不会填满屏幕。
我想旋转 web View ,因为 AndroidTV 需要横向模式......但我们将电视设置为纵向。
我用 fill_parent && match_parent但它在任何一种情况下都不起作用。
这是 Android Studio 预览的屏幕截图。
还要注意,当我这样测试时,似乎 webview 与屏幕大小匹配但不适应旋转......它只是旋转 webview 而不会将其调整为新的尺寸
rotated webview not filling parent

<?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 建议的代码和布局然后它起作用了......干杯

最佳答案

您遇到的问题是布局后发生旋转。这意味着在横向模式下进行测量(宽度 > 高度),然后旋转 View ,但新宽度是旧高度,新高度是旧宽度。这就是你所看到的,它不是一个错误。
解决此问题的一种方法是创建一个自定义 View ,该 View 交换宽度和高度并在原点旋转 -90 度,然后将整个 View 向下平移所需的高度。
这是自定义 View 的代码:
旋转的WebView.kt

class RotatedWebView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : WebView(context, attrs, defStyleAttr) {

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val desiredWidth = MeasureSpec.getSize(heightMeasureSpec)
val desiredHeight = MeasureSpec.getSize(widthMeasureSpec)
translationY = desiredWidth.toFloat()
setMeasuredDimension(desiredWidth, desiredHeight)
}
}
旋转WebView.java
public class RotatedWebView extends WebView {
public RotatedWebView(Context context) {
super(context);
}

public RotatedWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public RotatedWebView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int desiredWidth = MeasureSpec.getSize(heightMeasureSpec);
int desiredHeight = MeasureSpec.getSize(widthMeasureSpec);
setTranslationY(desiredWidth);
setMeasuredDimension(desiredWidth, desiredHeight);
}
}
布局将如下所示:
activity_main.xml
<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:orientation="vertical"
tools:context=".MainActivity">

<FrameLayout
android:id="@+id/frameParent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:background="@android:color/transparent"
android:foregroundGravity="center"
android:keepScreenOn="true">

<com.example.webviewrotation.RotatedWebView
android:id="@+id/keypadWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rotation="-90"
android:transformPivotX="0dp"
android:transformPivotY="0dp" />

</FrameLayout>
</RelativeLayout>
这是在模拟器中看到的结果:
enter image description here
最好将所有 View 操作放在自定义 View 中,而不是在代码和 XML 之间拆分它。我假设您将物理旋转电视,以便网页显示如您所愿。

关于android - 如何用旋转的 webview 填充屏幕?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64597196/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com