我需要在Android应用程序中处理方向更改.为此我决定使用OrientationEventListener便利课.但他的回调方法给出了一些奇怪的行为.
我的应用程序以纵向模式启动,然后最终切换到lanscape.我在回调onOrientationChanged方法中执行了一些自定义代码,它提供了一些额外的UI处理逻辑 - 它有几个调用findViewById.奇怪的是,当从横向切换回纵向模式时,onOrientationChanged回调被调用两次,更糟糕的是 - 第二次调用处理错误 Context - findViewById方法开始返回null.这些调用是从MainThread进行的
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new OrientationListener();
}
@Override
protected void onResume() {
super.onResume();
// enabling listening
listener.enable();
}
@Override
protected void onPause() {
super.onPause();
// disabling listening
listener.disable();
}
Run Code Online (Sandbox Code Playgroud)
我用一个Activity没有任何逻辑的假人复制了相同的行为,除了一个处理定向哈希的人.我通过按Ctrl + F11从Android 2.2模拟器启动方向切换
可能有什么问题?
Upd:实现的内部类 OrientationEventListener
private class OrientationListener extends OrientationEventListener {
public OrientationL() {
super(getBaseContext());
}
@Override
public …Run Code Online (Sandbox Code Playgroud) android screen-orientation android-emulator android-2.2-froyo
我有一个UIWebView我想加载不同的URL取决于它的肖像或风景.我如何弄清楚我目前的方向是viewWillAppear什么?
uiwebview orientation screen-orientation orientation-changes ios
我创建了一个虚拟的Android设备,480x800运行它,并且因为我已经找到了如何更改屏幕orientation(纵向横向,CTRL+F11/ CTRL+F12/ KP7/ KP9),我使用这些键来更改orientation.
但是当我尝试其中一个键时,虚拟屏幕会旋转90度,但方向不会改变.因此,按钮垂直放置在屏幕上,所有文本也是如此.就像一张旋转90度的照片.
如何让虚拟学位实际切换到其他布局文件(/res/layout-land/activity_entry.xml)?Eclipse的图形查看器可以landscape/portrait正确切换.
编辑
我在此onCreateOptionsMenu(Menu menu)
方法下的活动中添加了此部分,但应用程序无法识别屏幕方向更改:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Context context = getApplicationContext();
CharSequence text = "Orientation changed";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
Run Code Online (Sandbox Code Playgroud)
编辑2
当我this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)从onCreate()方法中调用时,它运行良好.
android screen-orientation android-virtual-device android-emulator
我正在开发iOS 5目标项目.我有如下定位方法.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
NSLog(@" My Orientation");
return YES;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我的应用程序启动时,我的日志消息将在控制台上显示两次,即我Orientation将显示两次.我正在模拟器中测试我的应用程序,这只是在发布时发生,甚至没有更改orientation,然后再次更改方向.即
(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
Run Code Online (Sandbox Code Playgroud)
每次被调用两次,我认为这不是一个正确的行为.任何帮助都是
赞赏.
cocoa-touch objective-c screen-orientation uiinterfaceorientation ios
我有一个应用程序,显示活动中的全屏位图.为了提供快速加载时间,我将它们加载到内存中.但是当屏幕改变方向时,我想清除缓存,以便再次使用适合新维度的位图填充它.唯一的问题是,为了做到这一点,我需要检测何时发生方向变化.有谁知道如何检测这个?
我希望我的应用程序在纵向和反向纵向模式下工作。换句话说,我希望它在纵向模式下工作并使其可旋转180度!
我在清单中MainActivity的<activity>标记内使用以下代码。
android:screenOrientation="sensorPortrait"
Run Code Online (Sandbox Code Playgroud)
但是,这似乎仅适用于Android 4.1.2及更高版本。
在Android 4.2.2及更高版本上不起作用!
我在不同的电话上尝试过,结果相同!怎么解决呢?
android portrait rotation screen-orientation android-sensors
我为手机(iphone和android)创建了一个网页(chrome和safari),我想将屏幕方向锁定为纵向模式。
与移动应用程序不同,网页没有清单文件和活动。
如何使用技术(css / javascript / bootstrap / jquery)或其他技术锁定手机的方向?
android google-chrome screen mobile-safari screen-orientation
我试图处理保存fragment状态以避免屏幕旋转时出现问题.它发生了一件奇怪的事情:当我第一次旋转屏幕时一切正常,但是当我第二次旋转屏幕应用程序崩溃时:
这里是片段代码的一部分
//save information: a string and an image
@Override
public void onSaveInstanceState(Bundle bundle) {
super.onSaveInstanceState(bundle);
bundle.putString("namesurname", nameSurnameString);
bundle.putParcelable("imgprofile", bitmap);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
if (savedInstanceState != null) {
//Restore the fragment's state
this.nameSurname.setText(getArguments().getString("namesurname"));
this.profileImage.setImageBitmap((Bitmap)getArguments().getParcelable("imgprofile"));
}
}
Run Code Online (Sandbox Code Playgroud)
这里有部分活动代码
//save the fragment
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//THE NEXT ONE IS LINE THAT RAISES THE EXCEPTION WHEN I ROTATE THE SCREEN
//FOR THE SECOND TIME
getSupportFragmentManager().putFragment(outState, "profileFragment", profileFragment);
}
@Override
protected void onCreate(Bundle …Run Code Online (Sandbox Code Playgroud) 目标
我正在研究简化的音频播放器,如果用户离开应用程序(主页按钮,任务切换,任务中断,屏幕关闭等),则需要停止播放.我可以让音频继续在后台播放并打开一个带有暂停/停止选项的通知栏,但我的用户几乎没有计算机知识,有些人可能无法在没有帮助的情况下停止音频.
问题
为了停止音频,我需要知道在活动被销毁之前应用程序何时移出焦点.Android提供了两种方法onStop和onPause,它们表明正在发生的事情,但不是什么.如果可能,我如何知道活动何时因方向改变而被销毁?
解决方案失败
我可以在onPause(或onStop/onDestroy)中使用isFinishing()来检查应用程序是否正在关闭并停止音频.但这只会告诉我们申请何时结束.它不区分移动到背景和旋转.
IsChangingConfigurations是一个有趣的函数,可以解决我的问题,但这需要API 11,我有兴趣支持API 10.
另一个解决方案似乎是一个糟糕的黑客.与此解决方案一样,OrientationChangeListener 看起来很理想,但它只注意到活动关闭并重新启动后方向已更改.
与上述选项类似,onConfigurationChanged仅在完成方向更改后报告方向更改.
有缺陷但可能的解决方案
一种解决方案是当媒体播放器失去与活动的连接时通知媒体播放器服务,然后在一段时间后自动暂停.这绝对是一个可行的解决方案,但它并不理想.多少时间合适?如果平板电脑运行缓慢且旋转时间比平时长,会发生什么?如果没有更好的选择,我会回到这个解决方案.这似乎是一个黑客,容易出错.
对于我当前的项目,我需要在横向模式下为移动设备优化页面布局。您可以帮助我了解测量浏览器窗口大小的不同方法吗?
我正在使用具有像素硬件像素尺寸的Android智能手机720 x 1280。
纵向模式
在纵向模式下,当我使用JavaScript来获取document.documentElement.clientWidth和〜Height时,会得到结果980 x 1394。
当我使用以下CSS ...
html {
height: 100vh;
width: 100vw;
}
body {
height: 100%;
width: 100%;
margin: 0;
}
Run Code Online (Sandbox Code Playgroud)
... Chrome开发工具报告说它的大小为980 x 1546。
横向模式
在横向模式下,情况似乎更加复杂。在测试中,我明确地将整个<html>标签的CSS尺寸设置为100vwx 100vh,将bodywidth和height设置为100%。
但是,JavaScript报告的clientWidth和clientHeight为980 x 460,而Chrome开发工具将html和body元素的尺寸显示为980px x 556px,尽管这两个元素均不能填充屏幕的宽度或高度。
甲<main>元件,其宽度被设定为200vh,其高度设定为100vh填充在横向模式下的屏幕的整个宽度,但叶片在垂直方向上的间隙,尽管铬报告它具有的尺寸的事实1112px x 556px。
了解不同尺寸属性到底要测量什么也非常有帮助,这样我就可以理解应如何使用它们。
编辑: …
android ×7
ios ×2
aspect-ratio ×1
cocoa-touch ×1
css ×1
javascript ×1
mobile ×1
objective-c ×1
orientation ×1
portrait ×1
rotation ×1
screen ×1
uiwebview ×1