【问题标题】:WebView appears white while progressbar loads加载进度条时 WebView 显示为白色
【发布时间】:2013-05-02 09:40:46
【问题描述】:

我有一个ProgressBar,它在WebView 加载时显示。我希望ProgressBar 以小尺寸显示,而背景为黑色。然后我可以在 WebView 加载后关闭进度条。

我遇到的问题是WebView 在加载时是白色的。有什么办法让它变黑吗?如果我将WebView 的背景颜色设置为黑色,那么短暂的一秒钟,它仍然是白色,并且使加载非常难看。

Activity的布局是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

    <WebView
        android:id="@+id/WebView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

    <ProgressBar
        android:id="@+id/ProgressBar"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:visibility="gone" />

</RelativeLayout>

或者,我应该使用某种类似于 Android 进度条的图像吗?

【问题讨论】:

    标签: android android-webview android-progressbar


    【解决方案1】:

    将以下内容添加到您的 XML 中:

    <WebView
        android:id="@+id/webview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff000000" />
    

    并在代码中执行此操作:

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.feed_fragment, container, false);
    
        WebView webView = (WebView) view.findViewById(
                R.id.webview);
        webView.setBackgroundColor(0);
    
        return view;
    }
    

    【讨论】:

    • 在代码中设置它对我有用。我已经更新了答案,你可以试试吗?
    • 如果我尝试这个,白屏出现的时间会更短,尽管仍然存在
    • 我的 webview 立即变黑,因为我在 onCreateView 结束之前更新了颜色。你可以发布你的 onCreateView 方法的内容吗? webview一开始是白色的吗?添加一些断点:webview什么时候从白变黑?
    • 太完美了,罗吉尔。有用。在 onCreate() 延迟一段时间后,我正在设置背景颜色。谢谢。
    【解决方案2】:

    在你的 relativeLayout 中添加这个

    <RelativeLayout 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" 
    android:background="#000000">
    

    【讨论】:

    • 你可以做一件事,首先将webView的可见性设置为不可见,直到进度条加载,此时进度条被关闭时,将可见性设置为可见
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 2012-06-29
    • 2012-03-11
    相关资源
    最近更新 更多