Android / WebView - 更改方向

时间:2013-11-05 06:50:32

标签: javascript android webview screen-orientation

我在活动中有webView。在内部,webView我有一个按钮,可以打开一个新的HTML页面。按下此按钮并打开新的html页面时,我希望屏幕方向更改为水平。

var searchButton = $('<button/>',
            {
                text:'Search!',
                "class":"buttons",
                id: "searchButtonID",
                click: function(){

                var select = document.getElementById("subCategory");
                var option = select.options[select.selectedIndex];
                var subCategoryId = option.id;
                window.location.href = "deals.html?subCategoryId=" + subCategoryId; 

                Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);             
            }
        });

我最近了解了允许Javascript与Android进行互动的WebAppInterfaces。我尝试添加这一行:

Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

但是,我收到一条错误消息:

 Uncaught ReferenceError: ActivityInfo is not defined

我在相关课程中导入了import android.content.pm.ActivityInfo;

不确定我正在做的事情是否可能......

非常感谢任何帮助!

3 个答案:

答案 0 :(得分:0)

 private void rotate(float degree) {
    final RotateAnimation rotateAnim = new RotateAnimation(0.0f, degree,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f,
            RotateAnimation.RELATIVE_TO_SELF, 0.5f);

    rotateAnim.setDuration(0);
    rotateAnim.setFillAfter(true);
    oWebView.startAnimation(rotateAnim);
}

btn.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {


            rotate(90);
        }
    });

试试这个

答案 1 :(得分:0)

做这样的事情

boolean rotationRequired = false;    

private void openWebviewDialog(String days)
{
dialog = new Dialog(MainActivity.GLOBAL_CONTEXT);
dialog.setContentView(R.layout.simple_webview);
WebView wb = (WebView) dialog.findViewById(R.id.simple_webview);
WebSettings webSettings = wb.getSettings(); 
wb.setWebViewClient(new MyWebViewClient());
webSettings.setJavaScriptEnabled(true);
wb.addJavascriptInterface(this, "javaScriptInterface");
String url = "http://www.yourdomain.com" // your url here
String title = "Hello";

wb.loadUrl(url);
dialog.setCancelable(true);
dialog.setTitle(title);
dialog.show();
}

class MyWebViewClient extends WebViewClient 
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) 
{
    if(url.contains("openExternal"))
    { 
        return super.shouldOverrideUrlLoading(view, url); // Leave webview and use browser
    } 
    else 
    {
        view.loadUrl(url); // Stay within this webview and load url
        return true;
    }
}

@Override
public void onPageFinished(WebView view, String url) 
{
    super.onPageFinished(view, url);
    if(rotationRequired)
    {
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }
}

@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) 
{
    super.onPageStarted(view, url, favicon);
}
}

@JavascriptInterface
public void passResult(String status) 
{
   rotationRequired = status.equalsIgnoreCase("true") ? true : false;
}

并且不要忘记将其添加到您的清单中:

android:configChanges = "orientation"

将旋转控制为

 <p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>

答案 2 :(得分:0)

请定义并导入包

    WebView myWebView;
    WebAppInterface mWebAppInterface;

将此代码添加到活动的onCreate()

mWebAppInterface = new WebAppInterface(this);
myWebView = (WebView) findViewById(R.id.webview);
myWebView.addJavascriptInterface(mWebAppInterface, "Android1");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//load your URL by myWebView.loadUrl("www.example.com");

还添加此公共功能

@JavascriptInterface
public void setorientation(){

     setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

现在您可以调用此公共函数来设置javascript按钮单击功能的方向

Android1.setorientation();

也参观  http://developer.android.com/guide/webapps/webview.html  using javascript in android webview