活动不自动旋转:
<activity
android:name=".E028"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Black.NoTitleBar" >
</activity>
如果我使用 onConfigurationChanged ,它不起作用:
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
当活动不自动旋转时,如何检查手机状态是横向还是纵向?
我认为这种情况:我必须使用传感器
My code:
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
if (event.values[1] < 6.5 && event.values[1] > -6.5) {
if (orientation != 1) {
Log.d("Sensor", "Landscape");
// listImage.notifyDataSetChanged();
}
orientation = 1;
} else {
if (orientation != 0) {
Log.d("Sensor", "Portrait");
// listImage.notifyDataSetChanged();
}
orientation = 0;
}
}
但它工作得不是最好,我的手机状态是纵向:如果向后倾斜一点,返回值是横向。
尝试这个..
getResources().getConfiguration()
将返回Configuration http://developer.android.com/reference/android/content/res/Configuration.html
if(YourActivity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{
// Portrait Mode
} else {
// Landscape Mode
}
EDIT
锁定到一个方向时获取手机方向 https://stackoverflow.com/questions/14836003/get-phone-orientation-when-locked-into-one-orientation
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)