根据高度和宽度设置设备的方向

时间:2015-03-18 11:28:33

标签: android android-layout android-activity android-ui

在我的应用程序中,我的情况是所有手机都只有肖像,平板电脑高度= 1024;宽度= 768然后高度/宽度(1024/768 <= 1.3333)我想制作标签横向否则我需要制作肖像。

String userAgent = new WebView(activity).getSettings()
            .getUserAgentString();
double screen_size = 1.3333333333333333;
if (userAgent.contains("Mobile")) {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        mEditor.putString("Device_Mode", "Phone");
        mEditor.commit();
    } else {
        activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        Display display = activity.getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getRealSize(size);
        double height = size.y;
        double width = size.x;
        double aspect_Ratio = width / height;
        if (aspect_Ratio <= screen_size) {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
            mEditor.putString("Device_Mode", "TAB-LANDSCAPE");
            mEditor.commit();
            quit();
        } else {
            activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
            mEditor.putString("Device_Mode", "TAB-PORTRAIT");
            mEditor.commit();
            quit();
        }
}

屏幕正在轻弹,应用程序终于崩溃了。

2 个答案:

答案 0 :(得分:0)

使用以下代码检查手机或平板电脑的天气。

TelephonyManager manager =(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
if(manager.getPhoneType() == TelephonyManager.PHONE_TYPE_NONE){
return "Tablet";
}else{
return "Mobile";
}

答案 1 :(得分:0)

屏幕闪烁,因为它在方向之间不断切换。

所以我已经停止了
android:configChanges="orientation|keyboardHidden|screenSize" 

将此行代码放在相应活动的清单中。因此,活动闪烁停止。

int i = getResources().getConfiguration().orientation;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getRealSize(size);
double height = size.y;
double width = size.x;
double aspect_Ratio = 0.0;
if (i == 1)
    aspect_Ratio = height / width;
else if (i == 2)
    aspect_Ratio = width / height;
if (aspect_Ratio <= screen_size) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

通过使用此代码,我完成了我的任务:)