问题
im working on an android app and im dealing with a webview inside of my android app. i have two layout mainly the activitymain.xml found in layout folder and another layout named activitymain.xml found in layout-land folder and im loading these two from 1 java file but i need these two to load two different webview one for landscape and one for portrait.my question is is it possible to directly put the url/file path of my webview in xml so that when the user change orientation and the activitymain.xml in layout-land load he load a webview for landscape and not the webview that is in portrait xml?
UPDATE:
i was able to run to it on my own terms problem is that it loads again from the start that was a stupid mistake i didnt put into account when the change occurs the app loads a new page which was the plan from that start but the problem is the data of the user before the change is lost and therefor i think that the solution i had is not a solution but a problem can you advice me on how to make change in orientation without the loss of data and to continue where in the last page where the change happened not to start from the beginning again ?
回答1:
I think he is trying to find some way to set the url directly in the xml-file without having to run the method loadUrl in the Activity. Like: <Webview android:url="@string/my_url" />
But i dont think that is possible. See the official documentation Handling Runtime Changes
Changing it will actually create a new view.
Instead you can do it like this programmatically:
in your onCreate
method code it like this
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*getResources().getConfiguration().ORIENTATION_PORTRAIT;*/
webview = (WebView)findViewById(R.id.webView1);
webview.getSettings().setJavaScriptEnabled(true);
webview.setWebViewClient(new WebViewClient()
{
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getApplicationContext(), description, Toast.LENGTH_SHORT).show();
}
});
webview.loadUrl("http://www.facebook.com");
}
Furthermore you can check orientation change via:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
webview.loadUrl("http://www.google.com");
Toast.makeText(getApplicationContext(), "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
webview.loadUrl("http://www.stackoverflow.com");
Toast.makeText(getApplicationContext(), "portrait", Toast.LENGTH_SHORT).show();
}
}
and put this in your manifest.xm
l <activity>
android:configChanges="orientation|screenSize"
Hopefully this will work for you. works fine for me.
Edit:
Dont forget to give the Internet Permission else it wont run
<uses-permission android:name="android.permission.INTERNET"/>
Else if any problem, then do let me know.
Thank you.
来源:https://stackoverflow.com/questions/19994140/landscape-view-and-portrait-view-of-webview-in-android