Webview方向无法正常工作

时间:2010-08-01 06:46:08

标签: android webview screen-orientation screen-rotation

这是我的代码。一切都很好,除了方向,它重新加载每次我旋转手机重新加载到主页,这是一个痛苦。任何人都可以告诉我问题在哪里。

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.Toast;


public class hpro extends Activity {
/** Called when the activity is first created. */
WebView webview;

//zoom

private static final FrameLayout.LayoutParams ZOOM_PARAMS =

    new FrameLayout.LayoutParams(

    ViewGroup.LayoutParams.FILL_PARENT,

    ViewGroup.LayoutParams.WRAP_CONTENT,

    Gravity.BOTTOM);
//finish zoom

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




//progress bar

getWindow().requestFeature(Window.FEATURE_PROGRESS);

// frame starts
setContentView(R.layout.main);

webview = (WebView) findViewById(R.id.webview); 
WebSettings webSettings = webview.getSettings(); 
webview.getSettings().setPluginsEnabled(true);
webview.setWebViewClient(new HelloWebViewClient() {}); // MARK
webSettings.setJavaScriptEnabled(true); 



//zoom 
FrameLayout mContentView = (FrameLayout) getWindow().
getDecorView().findViewById(android.R.id.content);
final View zoom = this.webview.getZoomControls();

mContentView.addView(zoom, ZOOM_PARAMS);

zoom.setVisibility(View.GONE);

//end zoom
//loading bar


final Activity activity = this;
webview.setWebChromeClient(new WebChromeClient() {
  public void onProgressChanged(WebView view, int progress) {
    // Activities and WebViews measure progress with different scales.
    // The progress meter will automatically disappear when we reach 100%
    activity.setProgress(progress * 100);
  }
});
webview.setWebViewClient(new WebViewClient() {
  public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
    Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
  }
});

webview.loadUrl("http://mypage.com/"); 

}

//orientation

@Override
public void onConfigurationChanged(Configuration newConfig){        
   super.onConfigurationChanged(newConfig);
}


//block browser

private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}


// back key

public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

//start submenu


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}


public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.home:
webview.loadUrl("http://mypage.com");
return true;

case R.id.exit:
// TODO

Intent i = new Intent();
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_HOME);
this.startActivity(i);
return true;

case R.id.reg:
webview.loadUrl("http://mypage.com/subpage");
return true;

case R.id.help:
webview.loadUrl("http://mypage.com/help");
return true;

}

return false;
}
}

2 个答案:

答案 0 :(得分:2)

机器人:configChanges = “取向” 在android manifest中声明活动时添加

答案 1 :(得分:0)

您的问题是您没有正确处理方向,如果您希望它保持状态,您需要确保覆盖onSaveInstanceState中的onRestoreInstanceStateActivity方法正确。请查看this answerthis answer中的信息。