Android版|获取屏幕高度宽度和屏幕宽度为高度 [英] Android | Getting screen height as width and screen width as height

查看:659
本文介绍了Android版|获取屏幕高度宽度和屏幕宽度为高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面这段code在我的应用程序获取屏幕宽度和高度:

I have the following piece of code in my app to get screen width and height:

screenHeight = (short) Activity.getWindow().getWindowManager().getDefaultDisplay().getHeight();
screenWidth  = (short) Activity.getWindow().getWindowManager().getDefaultDisplay().getWidth();

而在AndroidManifest,我有:

And in the AndroidManifest, I have:

 <activity
      android:name="com.test.MyActivity"
      android:configChanges="keyboardHidden|orientation"
      android:launchMode="singleTask"
      android:screenOrientation="landscape"
      android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
 </activity> 

现在,与屏幕分辨率1280x700的设备,我希望屏幕宽度变量总是有值1280和screenHeight是700这是不论如何,用户可拿着手机。

Now, for a device with screen resolution 1280x700, I expect the screenWidth variable to always have the value 1280 and the screenHeight to be 700. This is regardless of how the user may be holding the phone.

但尽管如此,有时我得到屏幕宽度等于700和高度等于1280,这是为什么?应该不是我的清单文件执行我的应用程序的取向始终保持景观?

But still, sometimes I get screenWidth equal to 700 and height equal to 1280. Why is that? Shouldn't my manifest file enforce the orientation of my app to always remain landscape?

推荐答案

放线在你的清单活动标签

put the line in your manifest activity tag

android:configChanges="keyboardHidden|orientation"

覆盖的方法在你的活动

override the method in your activity

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
}

和放线onCreate()方法

and put the line in onCreate() method

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAP);

您的应用程序将保持在仅landscap。 :)

your application will remain in landscap only. :)

这篇关于Android版|获取屏幕高度宽度和屏幕宽度为高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆