如何仅为手机锁定屏幕方向?

时间:2016-04-12 07:24:05

标签: android screen-orientation

在android中,可以通过将其添加到清单来锁定屏幕方向:

android:screenOrientation="landscape"

但是可以锁定大小吗?

我希望我的应用程序被锁定为手机格式的肖像,我希望用户能够以平板电脑格式同时使用纵向和横向。

我尝试使用屏幕尺寸:

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);

但是当我想从代码锁定屏幕时它似乎不起作用。想知道这样做的好方法吗?

5 个答案:

答案 0 :(得分:2)

首先检查this回答

现在你可以像这样改变方向

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (!tabletSize) {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

答案 1 :(得分:1)

使用此:

public static boolean isTablet(Context context) {
    return (context.getResources().getConfiguration().screenLayout
            & Configuration.SCREENLAYOUT_SIZE_MASK)
            >= Configuration.SCREENLAYOUT_SIZE_LARGE;
}

public void setOrientation(Context context){
if(!isTablet(contex){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }
}

答案 2 :(得分:1)

您可以按照

获取设备的对角线值
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

float yInches= metrics.heightPixels/metrics.ydpi;
float xInches= metrics.widthPixels/metrics.xdpi;
double diagonalInches = Math.sqrt(xInches*xInches + yInches*yInches);

之后,如果您想要5,5.5,6,6.5或更高的尺寸并根据需要改变方向,您可以根据需要进行检查

if (diagonalInches>=6.5){
        // 6.5inch device or bigger
 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    }else{
        // smaller device
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    }

答案 3 :(得分:1)

您可以创建自定义方法来检查手机的当前屏幕密度,

if (!isTabLayout(this)) {
   setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
  }

以纵向锁定屏幕方向,

if (!isTabLayout(this)) {
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  }

或横向,

public class Person : INotifyPropertyChanged, IEditableObject
{
    private bool isEditing;
    private bool isChanged;

    public string Name
    {
        get { return name; }
        set
        {
            if (name != value)
            {
                name = value;
                OnPropertyChanged();
            }
        }
    }
    private string name;

    public int Age
    {
        get { return age; }
        set
        {
            if (age != value)
            {
                age = value;
                OnPropertyChanged();
            }
        }
    }
    private int age;

    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
    {
        if (isEditing)
        {
            isChanged = true;
        }
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion

    #region IEditableObject

    public void BeginEdit()
    {
        isEditing = true;
    }

    public void CancelEdit()
    {
        isEditing = false;
    }

    public void EndEdit()
    {
        if (isChanged)
        {
            // generate and execute update SQL here
        }
        isEditing = false;
    }

    #endregion
}

答案 4 :(得分:0)

我不确切知道它的工作与否尝试这个

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

或查看一次Determine if the device is a smartphone or tablet?