[英]Android/WebView - Change Orientation
I have a webView
in an Activity. 我在活动中有一个
webView
。 Inside, the webView
I have a button that opens a new HTML
page. 在
webView
内部,我有一个打开新HTML
页面的按钮。 When this button is pressed and the new html page opens I would like the screen orientation to change to horizontal. 当按下此按钮并打开新的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);
}
});
I recently learnt about WebAppInterfaces
that allows Javascript to interact with Android. 我最近了解了
WebAppInterfaces
,它允许Javascript与Android进行交互。 I tried adding the line: 我尝试添加以下行:
Android.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
However, I get an error message saying: 但是,我收到一条错误消息:
Uncaught ReferenceError: ActivityInfo is not defined
I imported import android.content.pm.ActivityInfo;
我导入
import android.content.pm.ActivityInfo;
in the relevant class. 在相关课程中。
Not sure if what I'm doing is even possible... 不知道我在做什么甚至有可能...
Any help would be greatly appreciated! 任何帮助将不胜感激!
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);
}
});
try this 尝试这个
Do something like this 做这样的事情
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;
}
and don't forget to add this to your manifest: 并且不要忘记将其添加到清单中:
android:configChanges = "orientation"
control the rotation as 控制旋转为
<p>rotate...</p><script type='text/javascript' language='javascript'>javaScriptInterface.passResult('true');</script>
Please define and import packages 请定义并导入包
WebView myWebView;
WebAppInterface mWebAppInterface;
add this code to onCreate() of your activity 将此代码添加到您活动的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");
Add this public function also 也添加此公共功能
@JavascriptInterface
public void setorientation(){
setRequestedOrientation (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
Now you can call this public function for setting orientation from javascript button click function 现在您可以通过javascript按钮单击功能调用此公共功能来设置方向
Android1.setorientation();
visit also http://developer.android.com/guide/webapps/webview.html using javascript in android webview 也可以在android webview中使用javascript访问http://developer.android.com/guide/webapps/webview.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.