Android Webiew App在加载网站前显示图片

时间:2016-03-01 09:57:36

标签: java android webview android-webview

我对Android App很新鲜。我完成了我的第一个Webview应用程序。现在想在网站加载之前添加加载图像(.jpg)。我什么都不知道。只是解释清楚。图片可能会添加来自其他来源的应用或图片。

我的 activity_main.xml

<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

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

</FrameLayout> 

AndroidManifest.xml 位于下方,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.steptoinstall.steptoinstall" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

MainActivity.java 在下方,

package com.steptoinstall.steptoinstall;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends ActionBarActivity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://www.steptoinstall.com");

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

         // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        return super.onOptionsItemSelected(item);
    }
}

2 个答案:

答案 0 :(得分:2)

我认为最简单的方法是在imageView上放置webView,然后在加载网站时隐藏它。

首先,你必须改变你的xml:

<FrameLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin">

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

    <ImageView
        android:id="@+id/activity_main_imageview"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />


</FrameLayout> 

并将此行添加到您的主要活动

mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
        // hide your imageView
    }
});

请告诉我你是否想了解更多详情。

更新2: 在MainActivity中,在setContentView之后添加:

final ImageView mImageView = (ImageView)findViewById(R.id.activity_main_imageview);

之后你可以在xml或代码中将图像设置为此imageView:

xml中的

更改了ImageView:

<ImageView
    android:id="@+id/activity_main_imageview"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:src="@drawable/img" />

并且你的MainActivity应该是这样的:

mWebView = (WebView) findViewById(R.id.activity_main_webview);

    mWebView = (WebView) findViewById(R.id.activity_main_webview);
    mWebView.loadUrl("http://www.steptoinstall.com");

    // Enable Javascript
    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

     // Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient() {

   public void onPageFinished(WebView view, String url) {
    mImageView.setVisibility(View.GONE);
    }
    });

答案 1 :(得分:0)

在加载主要活动之前添加启动画面。然后添加这个

import android.app.Activity;

import android.content.Intent; import android.os.Bundle;

import com.example.tabs.R;

公共类Splash extends Activity实现了Runnable {

Thread mThread;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.splash);

    mThread = new Thread(this);

    mThread.start();
}

@Override
public void run() 
{
    try
    {
        Thread.sleep(2000);
    }
    catch (Exception e) 
    {
        e.printStackTrace();
    }
    finally
    {
        startActivity(new Intent(Splash.this, MainActivity.class));

        finish();
    }
}

}

和xml

&#13;
&#13;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/splash" >


</LinearLayout>
&#13;
&#13;
&#13;