我知道有很多类似的问题,相信我,我尝试了很多,但对我来说没有任何效果。我有一个使用 Webview 的 Android 应用程序。您可以在 Play 商店中找到我的应用程序:
在 Play 商店呆了几个月后,我决定进行更新,并为我的新设计使用 Android 设计指南,因此操作栏是必要的。我选择了 ActionbarSherlock,一切都很好,除了 Webview 现在每次我旋转手机时都会重新加载。在我的上一个版本中,我用这个答案处理了这个问题并且它有效:
但现在它不会再工作了。我也尝试过该解决方案:(我还在网站末尾留下了评论)
同样令人失望的结果。我什至不确定 ActionbarSherlock 是否是问题所在,因为我已经更改了很多,但是无法重新创建旧状态!
这是应用程序的外观(仅在新版本中实现,您无法在 Play Store 上发布的版本中找到它):
这是我的代码的一部分,首先是清单,然后是布局,然后是 java 代码:
显现:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.rk.rps"
android:versionCode="8"
android:versionName="0.4" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<application
android:icon="@drawable/rps"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar" >
<activity
android:name=".RPSActivity"
android:configChanges="orientation"
android:label="@string/app_name" >
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name=".DonateActivity"
android:label="@string/app_name" >
</activity>
<activity
android:name=".PayPalActivity"
android:configChanges="orientation"
android:label="@string/app_name" >
</activity>
<activity
android:name=".LoadingScreen"
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>
</manifest>
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/paypal_loadingtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:gravity="center"
android:text="@string/paypal_loadingtext"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/rk_red" />
<ProgressBar
android:id="@+id/paypal_progressbar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/paypal_loadingtext"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="10dp" />
<WebView
android:id="@+id/paypal_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="gone" />
</RelativeLayout>
Java代码:
package at.rk.rps;
import org.apache.http.util.EncodingUtils;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
public class PayPalActivity extends SherlockActivity {
private ActionBar actionbar;
private WebView webview;
private TextView loadingtxt;
private ProgressBar progressbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(getSharedPreferences(RPSActivity.PREFS_NAME, 0).getBoolean(RPSActivity.FULLSCREEN, true)) this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.paypal);
initUI();
}
private void initUI() {
actionbar = getSupportActionBar();
actionbar.setTitle(R.string.paypal_actionbar_headline);
actionbar.setHomeButtonEnabled(true);
this.actionbar.setDisplayHomeAsUpEnabled(true);
loadingtxt = (TextView) findViewById(R.id.paypal_loadingtext);
progressbar = (ProgressBar) findViewById(R.id.paypal_progressbar);
webview = (WebView) findViewById(R.id.paypal_webview);
webview.requestFocus(View.FOCUS_DOWN); // Enables the keyboard in landscape mode
webview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_UP:
if (!v.hasFocus()) {
v.requestFocus();
}
break;
}
return false;
}
});
webview.getSettings().setJavaScriptEnabled(true); // Enables JavaScript
webview.getSettings().setBuiltInZoomControls(true); // Enables zoom
webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); // Displays scrollbars outside the view
webview.setScrollbarFadingEnabled(false);
webview.getSettings().setLoadWithOverviewMode(true);
webview.getSettings().setUseWideViewPort(true);
webview.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
loadingtxt.setVisibility(TextView.VISIBLE);
progressbar.setVisibility(ProgressBar.VISIBLE);
webview.setVisibility(WebView.GONE);
progressbar.setProgress(progress);
if(progress == 100) {
loadingtxt.setVisibility(LinearLayout.GONE);
progressbar.setVisibility(LinearLayout.GONE);
webview.setVisibility(WebView.VISIBLE);
}
}
});
webview.setWebViewClient(new WebViewClient());
byte[] post = EncodingUtils.getBytes("cmd=_s-xclick&hosted_button_id=VRM63MEY4J936", "BASE64");
webview.postUrl("https://www.paypal.com/cgi-bin/webscr", post);
}
@Override
public void onConfigurationChanged(Configuration newConfig){
super.onConfigurationChanged(newConfig);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
if(webview.canGoBack() == true){
webview.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater i = getSupportMenuInflater();
i.inflate(R.menu.paypal_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case android.R.id.home:
finish();
return(true);
case R.id.paypal_actionbar_reload:
webview.reload();
return(true);
default:
return super.onOptionsItemSelected(item);
}
}
}