android中的webview的横向视图和纵向视图 [英] landscape view and portrait view of webview in android

查看:84
本文介绍了android中的webview的横向视图和纵向视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个android应用,并且正在处理我的android应用内的webview.我有两个布局,主要是在layout文件夹中找到的activitymain.xml,另一个是在layout-land文件夹中找到的名为activitymain.xml的布局,我从1个Java文件中加载了这两个布局,但是我需要这两个来加载两个不同的webview,一个用于风景,另一个对于portrait.my问题,是否有可能直接将我的Webview的url/文件路径放在xml中,以便当用户更改方向和在layout-land加载中的activitymain.xml时,他为风景加载Webview而不是在纵向xml中?

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?

更新:

我能够按自己的意愿运行它,问题是它从一开始就再次加载,这是一个愚蠢的错误,当发生更改时我没有考虑到该应用程序加载了一个新页面,这是该开始的计划但是问题是丢失更改之前用户的数据,因此我认为我所拥有的解决方案不是解决方案,但是有问题吗?您能建议我如何在不丢失数据的情况下进行方向更改并继续吗?在最后一次发生更改的页面不是从头开始吗?

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 ?

推荐答案

我认为他试图找到某种方法直接在xml文件中设置url,而不必在Activity中运行方法loadUrl.就像:< Webview android:url ="@ string/my_url"/>

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:

像这样

@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();
        }
    }

并将其放入您的 manifest.xm l < activity>

and put this in your manifest.xml <activity>

android:configChanges="orientation|screenSize"

希望这对您有用.对我来说很好.

Hopefully this will work for you. works fine for me.

别忘了授予Internet权限,否则它将无法运行

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.

谢谢.

这篇关于android中的webview的横向视图和纵向视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆