分屏模式下的橫豎屏並非爲非分屏模式下的橫豎屏,其橫豎屏爲非絕對的,具體的橫豎值根據分屏後的高度與寬度值大小而定。
//1.寬高判斷
DisplayMetrics dm = getResources().getDisplayMetrics()
int width = dm.widthPixels;
int height = dm.heightPixels;
//然後根據寬高的大小比,判斷是豎屏(portrait)還是橫屏(landscape)。
//2. 是否爲豎屏,或者也可以判斷是否爲橫屏。
if(getResources().getConfiguration().orientation==Configuration.ORIENTATION_PORTRAIT)
private void setHomepageAppBarPaddingTop() {
final View appbar_layout = this.findViewById(R.id.appbar_layout);
final int paddingTop=getResources().getDimensionPixelSize(R.dimen.appbar_padding);
ComponentName componentname = new ComponentName("com.android.>>>>", "om.android.>>>>.******");
if(isInMultiWindowMode() //分屏 &&!componentname.equals(Objects.requireNonNull(Utils.getSplitScreenPrimaryStack()).topActivity)){
Log.d(TAG, "setHomepageAppBarPaddingTop: " + Utils.getSplitScreenPrimaryStack().topActivity.toString()+ this.getResources().getConfiguration().orientation+"");
appbar_layout.setPadding(0,0,0,0);
}else{
appbar_layout.setPadding(0,paddingTop,0,0);
}
}
// 判斷當前分屏主堆棧
public static ActivityManager.StackInfo getSplitScreenPrimaryStack(){
try {
return ActivityTaskManager.getService().getStackInfo(WINDOWING_MODE_SPLIT_SCREEN_PRIMARY, ACTIVITY_TYPE_UNDEFINED);
} catch (RemoteException e) {
return null;
}
}
// 分屏監聽
@Override
public void onMultiWindowModeChanged(boolean isInMultiWindowMode) {
super.onMultiWindowModeChanged(isInMultiWindowMode);
}