簡體   English   中英

如何在移動設備中動態限制景觀方向?在平板電腦中使用相同代碼沒有限制?

[英]how do i restrict the landscape orientation dynamically in mobile ?and no restricion in tablet in the samecode?

    int currentOrientation = getResources().getConfiguration().orientation;
    if (currentOrientation == Configuration.ORIENTATION_PORTRAIT) {
       getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
    }
    else {
       getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    }

當我旋轉手機時,我的內容也在旋轉。在橫向和縱向模式下,我只需要相同的屏幕。

要檢測應用程序是在手機還是平板電腦上運行,請按照Marius的建議使用以下方法https://stackoverflow.com/a/9308284/923820

然后,將此代碼放在要防止方向更改的Fragment

@Override
public void onResume() {
    super.onResume();
    if (!tabletSize)
        getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}

當您的應用程序在手機上運行時,這會將屏幕鎖定為縱向模式。 如果您想將其鎖定為橫向模式,請更改為ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM