【问题标题】:HTC One X - Webview becomes white/empty after touchHTC One X - 触摸后网页视图变为白色/空白
【发布时间】:2012-06-23 11:11:18
【问题描述】:

我有一个 Android 应用程序,它有用于搜索的文本框、一些按钮、网络视图(1 可见 2 不可见)和 admob 广告视图。该应用程序仅在一些特殊网站上进行搜索,并且在模拟器、三星 Galaxy 设备上运行良好。但我对 HTC One X 有疑问(不知道其他 HTC 型号)。

问题是,当您单击搜索按钮时,webview 会加载页面。然后“有时”当您尝试触摸甚至滚动时,webview 只是返回到带有滚动条的完全白色区域。这尤其发生在广告刷新之后。甚至有时广告的背景和文本区域也完全变成白色-因此不可读-。在广告中,只有蓝色箭头和蓝色电话图像保持可见!

有没有人遇到过这样的问题,有什么解决办法吗?提前致谢。

这是我的布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/layoutDashboard"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:background="@drawable/border_shape"
            android:orientation="vertical"
            android:paddingTop="1sp" >

            <EditText
                android:id="@+id/editxtKeyword"
                android:layout_width="fill_parent"
                android:layout_height="43dp"
                android:layout_marginLeft="1dp"
                android:layout_marginRight="1dp"
                android:layout_marginTop="2dp"
                android:ems="10"
                android:hint="@string/editxtKeyword_hint"
                android:singleLine="true" >
            </EditText>

            <HorizontalScrollView
                android:id="@+id/horizontalScrollView1"
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_gravity="center_vertical"
                android:layout_weight="0.30"
                android:paddingBottom="2dp"
                android:paddingLeft="1dp"
                android:scrollbars="none" >

                <LinearLayout
                    android:id="@+id/layoutButtons"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_gravity="center_vertical"
                    android:layout_marginTop="0dp"
                    android:layout_marginBottom="1dp"
                    android:layout_marginLeft="1dp"
                    android:layout_marginRight="1dp"
                    android:orientation="horizontal" >
<
                    <Button
                        android:id="@+id/btnAk"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_margin="1dp"
                        android:background="@drawable/custom_button_shape"
                        android:text="asd" />

                    <Button
                        android:id="@+id/btnCm"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:layout_margin="1dp"
                        android:background="@drawable/custom_button_shape"
                        android:text="csm" />

                </LinearLayout>
            </HorizontalScrollView>
        </LinearLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <WebView
                android:id="@+id/webV"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_above="@+id/linlaylay1"
                android:visibility="visible" />

            <WebView
                android:id="@+id/webVAnalytics"
                android:layout_width="1dp"
                android:layout_height="0dp"
                android:visibility="invisible" />

            <WebView
                android:id="@+id/WebVSahibinden"
                android:layout_width="1dp"
                android:layout_height="0dp"
                android:visibility="invisible" />

            <LinearLayout
                android:id="@+id/linlaylay1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_margin="0dp"
                android:orientation="vertical"
                android:padding="0dp" >

                <com.google.ads.AdView
                    android:id="@+id/adViewSmall"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal|top"
                    ads:adSize="BANNER"
                    ads:adUnitId="a123123"
                    ads:loadAdOnCreate="true"
                    ads:testDevices="TEST_EMULATOR"  />
            </LinearLayout>
        </RelativeLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layoutProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center_horizontal"
        android:layout_marginBottom="50dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/progress_shape"
        android:padding="2dp"
        android:visibility="invisible" >

        <TextView
            android:id="@+id/lblLoading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="2dp"
            android:text="@string/msg_loading"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="#000000" />

        <ImageButton
            android:contentDescription="@string/dialog_cancel"
            android:id="@+id/btnStop"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/stop" />
    </LinearLayout>

</FrameLayout>

【问题讨论】:

  • 不要回头,要向前看!有一些出现错误的情况。但是htc发布了新设备,这个问题并没有出现在这个设备上。

标签: android webview htc-android


【解决方案1】:

我在 HTC 手机上看到了单个 webviews 的问题。 One of the comments in this bug thread 提出一些建议来帮助优化 WebView 性能。这个错误是关于 3D 变换的,但我发现评论的技术对一般的 WebViews 很有用。

This presentation at Google I/O 2012 还提供了一些关于 WebViews 主题的好信息。

两者都建议将动态内容放在“层”上。这意味着将这个 CSS 规则分配给元素:

  -webkit-transform: translate3d(0, 0, 0);

通过应用此规则,Android“告诉浏览器将图层预先合成到其自己的纹理上,以便以后可以(几乎)免费进行动画处理”这种行为不是免费的 - 它会消耗更多内存,所以我会申请明智的。上面链接的演示文稿很好地解释了原因。

在我的例子中,我可以通过将此规则应用于包含整个页面的 DIV 来避免“白屏”。页面有时会呈现白色一秒钟,然后呈现内容。我没有看到“白色”页面像我在应用这种技术之前那样永远存在。我没有看到失控的内存消耗。 YMMV。

我还建议您尽可能简化布局。说起来容易做起来难,但是可以找到一些好的建议in the docs

【讨论】:

  • 我已经阅读了一些技巧并尝试了许多方法来解决所有 HTC webview 问题。最后,这是唯一有效的方法。谢谢!
  • Daniel,很高兴听到这种方法对其他人有用。这是我遇到的最奇怪的问题之一。
【解决方案2】:

A known bug

您可以在 WebView 上禁用硬件加速,但请确保以不会破坏旧设备的方式这样做:

if(android.os.Build.VERSION.SDK_INT > android.os.Build.VERSION_CODES.HONEYCOMB){
    //mWebView is some WebView
    mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

注意事项

  • 视频无法在浏览器中播放
  • 滚动不会那么流畅

根据错误,这已在 Jelly Bean 中修复,但我无法亲自验证。

【讨论】:

    【解决方案3】:

    在显示硬件加速视图时会出现很多问题。我会重新设计您的布局,以不与 Web 视图或滚动视图重叠,或禁用硬件加速。

    【讨论】:

    • 兰斯,感谢您的回复。实际上没有 webviews 重叠,因为其中只有 1 个是可见的,其他的则不是。加载时,我在 EditText 区域上显示“加载”标签,这都是重叠的。禁用硬件加速是什么意思?
    • 您可以在代码中或完全在清单中关闭单个视图的硬件加速,这是 Android 的一项新功能,其中视图被转换为 GPU 纹理并以这种方式绘制。顺便说一句,我经常看到像相机预览这样的东西会干扰事物,即使它们是不可见的。如果你真的要像你正在做的那样堆叠视图,如果你使用消失而不是不可见,你会有一个更好的改变它的工作。这是我之前修复的错误。
    猜你喜欢
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2015-12-20
    • 2021-11-23
    相关资源
    最近更新 更多