簡體   English   中英

Android / WebView-更改方向

[英]Android/WebView - Change 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);             
            }
        });

我最近了解了WebAppInterfaces ,它允許Javascript與Android進行交互。 我嘗試添加以下行:

Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

但是,我收到一條錯誤消息:

 Uncaught ReferenceError: ActivityInfo is not defined

我導入import android.content.pm.ActivityInfo; 在相關課程中。

不知道我在做什么甚至有可能...

任何幫助將不勝感激!

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

嘗試這個

做這樣的事情

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>

請定義並導入包

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

可以在android webview中使用javascript訪問http://developer.android.com/guide/webapps/webview.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM