如何检查Android手机是横向还是纵向?
如何检查Android手机是横向还是纵向?
Answers:
用于确定要检索哪些资源的当前配置可从“资源” Configuration
对象中获得:
getResources().getConfiguration().orientation;
您可以通过查看其值来检查方向:
int orientation = getResources().getConfiguration().orientation;
if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
// In landscape
} else {
// In portrait
}
可以在Android Developer中找到更多信息。
android:screenOrientation="portrait"
),则无论用户如何旋转设备,此方法都将返回相同的值。在这种情况下,您将使用加速度计或重力传感器正确确定方向。
如果在某些设备上使用getResources()。getConfiguration()。orientation,则会出错。我们最初在http://apphance.com中使用了该方法。多亏了Apphance的远程日志记录,我们可以在不同的设备上看到它,并且我们看到碎片在这里发挥了作用。我看到了奇怪的情况:例如,在HTC Desire HD上交替显示肖像和正方形(?!):
CONDITION[17:37:10.345] screen: rotation: 270 orientation: square
CONDITION[17:37:12.774] screen: rotation: 0 orientation: portrait
CONDITION[17:37:15.898] screen: rotation: 90
CONDITION[17:37:21.451] screen: rotation: 0
CONDITION[17:38:42.120] screen: rotation: 270 orientation: square
或根本不改变方向:
CONDITION[11:34:41.134] screen: rotation: 0
CONDITION[11:35:04.533] screen: rotation: 90
CONDITION[11:35:06.312] screen: rotation: 0
CONDITION[11:35:07.938] screen: rotation: 90
CONDITION[11:35:09.336] screen: rotation: 0
另一方面,width()和height()总是正确的(由窗口管理器使用,因此最好使用它)。我说最好的主意是总是进行宽度/高度检查。如果想一想,这正是您想要的-知道宽度是否小于高度(人像),对面(风景)还是相同(正方形)。
然后归结为以下简单代码:
public int getScreenOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
int orientation = Configuration.ORIENTATION_UNDEFINED;
if(getOrient.getWidth()==getOrient.getHeight()){
orientation = Configuration.ORIENTATION_SQUARE;
} else{
if(getOrient.getWidth() < getOrient.getHeight()){
orientation = Configuration.ORIENTATION_PORTRAIT;
}else {
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
}
return orientation;
}
getWidth
并且getHeight
不被弃用。
getSize(Point outSize)
代替。我使用的是API 23
解决此问题的另一种方法是不依赖于显示器的正确返回值,而是依赖Android资源解析。
在文件layouts.xml
夹中创建文件,res/values-land
并res/values-port
包含以下内容:
res / values-land / layouts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">true</bool>
</resources>
res / values-port / layouts.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape">false</bool>
</resources>
现在,您可以在源代码中按如下方式访问当前方向:
context.getResources().getBoolean(R.bool.is_landscape)
指定手机当前方向的完整方法:
public String getRotation(Context context){
final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
switch (rotation) {
case Surface.ROTATION_0:
return "portrait";
case Surface.ROTATION_90:
return "landscape";
case Surface.ROTATION_180:
return "reverse portrait";
default:
return "reverse landscape";
}
}
Chear Binh Nguyen
这是hackbod和Martijn推荐的如何获得屏幕方向的代码片段演示:
change更改方向时触发:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
int nCurrentOrientation = _getScreenOrientation();
_doSomeThingWhenChangeOrientation(nCurrentOrientation);
}
as按照hackbod的建议获取当前方向:
private int _getScreenOrientation(){
return getResources().getConfiguration().orientation;
}
get还有其他解决方案可获取当前屏幕方向❷遵循Martijn解决方案:
private int _getScreenOrientation(){
Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
return display.getOrientation();
}
★ 注意:我尝试同时实现❷和❸,但是在RealDevice(NexusOne SDK 2.3)方向上,它返回错误的方向。
★因此,我建议使用解决方案 ❷来获得具有更大优势的屏幕方向:清晰,简单并且像魅力一样工作。
★仔细检查定向返回以确保符合我们的预期(可能有限,具体取决于物理设备规格)
希望对您有所帮助,
int ot = getResources().getConfiguration().orientation;
switch(ot)
{
case Configuration.ORIENTATION_LANDSCAPE:
Log.d("my orient" ,"ORIENTATION_LANDSCAPE");
break;
case Configuration.ORIENTATION_PORTRAIT:
Log.d("my orient" ,"ORIENTATION_PORTRAIT");
break;
case Configuration.ORIENTATION_SQUARE:
Log.d("my orient" ,"ORIENTATION_SQUARE");
break;
case Configuration.ORIENTATION_UNDEFINED:
Log.d("my orient" ,"ORIENTATION_UNDEFINED");
break;
default:
Log.d("my orient", "default val");
break;
}
自从大多数答案发布以来,已经过去了一段时间,并且现在使用了一些不赞成使用的方法和常量。
我更新了Jarek的代码以不再使用这些方法和常量:
protected int getScreenOrientation()
{
Display getOrient = getWindowManager().getDefaultDisplay();
Point size = new Point();
getOrient.getSize(size);
int orientation;
if (size.x < size.y)
{
orientation = Configuration.ORIENTATION_PORTRAIT;
}
else
{
orientation = Configuration.ORIENTATION_LANDSCAPE;
}
return orientation;
}
请注意,该模式Configuration.ORIENTATION_SQUARE
不再受支持。
我发现这在我测试过的所有设备上都是可靠的,与建议使用 getResources().getConfiguration().orientation
在运行时检查屏幕方向。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
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();
}
}
还有另一种方法:
public int getOrientation()
{
if(getResources().getDisplayMetrics().widthPixels>getResources().getDisplayMetrics().heightPixels)
{
Toast t = Toast.makeText(this,"LANDSCAPE",Toast.LENGTH_SHORT);
t.show();
return 1;
}
else
{
Toast t = Toast.makeText(this,"PORTRAIT",Toast.LENGTH_SHORT);
t.show();
return 2;
}
}
不管用户是否设置了纵向方向,都已在2019年对API 28进行了测试,并且与另一个过时的答案相比,使用最少的代码即可实现正确的方向:
/** @return The {@link Configuration#ORIENTATION_SQUARE}, {@link Configuration#ORIENTATION_PORTRAIT}, {@link Configuration#ORIENTATION_LANDSCAPE} constants based on the current phone screen pixel relations. */
private int getScreenOrientation()
{
DisplayMetrics dm = context.getResources().getDisplayMetrics(); // Screen rotation effected
if(dm.widthPixels == dm.heightPixels)
return Configuration.ORIENTATION_SQUARE;
else
return dm.widthPixels < dm.heightPixels ? Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
}
我认为使用getRotationv()没有帮助,因为 http://developer.android.com/reference/android/view/Display.html#getRotation%28%29 getRotation()从其“自然”状态返回屏幕旋转取向。
因此,除非您知道“自然”方向,否则旋转是没有意义的。
我找到了一种更简单的方法
Display display = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
if(width>height)
// its landscape
请告诉我这个人是否有问题?
这样可以覆盖所有电话,例如oneplus3
public static boolean isScreenOriatationPortrait(Context context) {
return context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
}
正确的代码如下:
public static int getRotation(Context context){
final int rotation = ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getOrientation();
if(rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180){
return Configuration.ORIENTATION_PORTRAIT;
}
if(rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270){
return Configuration.ORIENTATION_LANDSCAPE;
}
return -1;
}
我知道的旧帖子。无论方向是什么,或方向如何,等等。我设计了此功能,该功能用于将设备设置为正确的方向,而无需了解设备上的纵向和横向特征是如何组织的。
private void initActivityScreenOrientPortrait()
{
// Avoid screen rotations (use the manifests android:screenOrientation setting)
// Set this to nosensor or potrait
// Set window fullscreen
this.activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
DisplayMetrics metrics = new DisplayMetrics();
this.activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
// Test if it is VISUAL in portrait mode by simply checking it's size
boolean bIsVisualPortrait = ( metrics.heightPixels >= metrics.widthPixels );
if( !bIsVisualPortrait )
{
// Swap the orientation to match the VISUAL portrait mode
if( this.activity.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT )
{ this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT ); }
}
else { this.activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR); }
}
奇迹般有效!
用这种方式
int orientation = getResources().getConfiguration().orientation;
String Orintaion = "";
switch (orientation)
{
case Configuration.ORIENTATION_UNDEFINED: Orintaion = "Undefined"; break;
case Configuration.ORIENTATION_LANDSCAPE: Orintaion = "Landscrape"; break;
case Configuration.ORIENTATION_PORTRAIT: Orintaion = "Portrait"; break;
default: Orintaion = "Square";break;
}
在字符串中,您拥有Oriantion
有很多方法可以做到这一点,这段代码对我有用
if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
// portrait mode
} else if (this.getWindow().getWindowManager().getDefaultDisplay()
.getOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
// landscape
}
我认为这种解决方案容易
if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT){
user_todat_latout = true;
} else {
user_todat_latout = false;
}
只需简单的两行代码
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
// do something in landscape
} else {
//do in potrait
}
简单容易:)
在java文件中,编写:
private int intOrientation;
在onCreate
方法和之前setContentView
写:
intOrientation = getResources().getConfiguration().orientation;
if (intOrientation == Configuration.ORIENTATION_PORTRAIT)
setContentView(R.layout.activity_main);
else
setContentView(R.layout.layout_land); // I tested it and it works fine.
还值得注意的是,如今,getResources().getConfiguration().orientation
出于布局原因,没有足够的理由来检查显式方向,因为Android 7 / API 24+中引入的多窗口支持可能会在两种布局中严重干扰您的布局取向。最好考虑使用<ConstraintLayout>
和取决于可用宽度或高度的替代布局,以及用于确定使用哪种布局的其他技巧,例如,是否将某些片段附加到您的Activity。
您可以使用此(基于此处):
public static boolean isPortrait(Activity activity) {
final int currentOrientation = getCurrentOrientation(activity);
return currentOrientation == ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT || currentOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
public static int getCurrentOrientation(Activity activity) {
//code based on https://www.captechconsulting.com/blog/eric-miles/programmatically-locking-android-screen-orientation
final Display display = activity.getWindowManager().getDefaultDisplay();
final int rotation = display.getRotation();
final Point size = new Point();
display.getSize(size);
int result;
if (rotation == Surface.ROTATION_0
|| rotation == Surface.ROTATION_180) {
// if rotation is 0 or 180 and width is greater than height, we have
// a tablet
if (size.x > size.y) {
if (rotation == Surface.ROTATION_0) {
result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a phone
if (rotation == Surface.ROTATION_0) {
result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
} else {
result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
}
}
} else {
// if rotation is 90 or 270 and width is greater than height, we
// have a phone
if (size.x > size.y) {
if (rotation == Surface.ROTATION_90) {
result = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
} else {
result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE;
}
} else {
// we have a tablet
if (rotation == Surface.ROTATION_90) {
result = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT;
} else {
result = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
}
}
}
return result;
}