下面列出了android.view.View#getFitsSystemWindows ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 支持actionBar的界面
* Support action bar.
*/
private void supportActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1()) {
for (int i = 0, count = mContentView.getChildCount(); i < count; i++) {
View childView = mContentView.getChildAt(i);
if (childView instanceof ViewGroup) {
mBarParams.systemWindows = childView.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
}
if (mBarParams.isSupportActionBar) {
mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight(), 0, 0);
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
}
}
/**
* 支持actionBar的界面
* Support action bar.
*/
private void supportActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1()) {
for (int i = 0, count = mContentView.getChildCount(); i < count; i++) {
View childView = mContentView.getChildAt(i);
if (childView instanceof ViewGroup) {
mBarParams.systemWindows = childView.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
}
if (mBarParams.isSupportActionBar) {
mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight(), 0, 0);
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
}
}
/**
* 支持actionBar的界面
* Support action bar.
*/
private void supportActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && !OSUtils.isEMUI3_1()) {
for (int i = 0, count = mContentView.getChildCount(); i < count; i++) {
View childView = mContentView.getChildAt(i);
if (childView instanceof ViewGroup) {
mBarParams.systemWindows = childView.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
}
if (mBarParams.isSupportActionBar) {
mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight(), 0, 0);
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
}
}
/**
* 检查布局根节点是否使用了android:fitsSystemWindows="true"属性
* Check fits system windows boolean.
*
* @param view the view
* @return the boolean
*/
public static boolean checkFitsSystemWindows(View view) {
if (view == null) {
return false;
}
if (view.getFitsSystemWindows()) {
return true;
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
View childView = viewGroup.getChildAt(i);
if (childView instanceof DrawerLayout) {
if (checkFitsSystemWindows(childView)) {
return true;
}
}
if (childView.getFitsSystemWindows()) {
return true;
}
}
}
return false;
}
/**
* 检查布局根节点是否使用了android:fitsSystemWindows="true"属性
* Check fits system windows boolean.
*
* @param view the view
* @return the boolean
*/
public static boolean checkFitsSystemWindows(View view) {
if (view == null) {
return false;
}
if (view.getFitsSystemWindows()) {
return true;
}
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0, count = viewGroup.getChildCount(); i < count; i++) {
View childView = viewGroup.getChildAt(i);
if (childView instanceof DrawerLayout) {
if (checkFitsSystemWindows(childView)) {
return true;
}
}
if (childView.getFitsSystemWindows()) {
return true;
}
}
}
return false;
}
private TutoShowcase(@NonNull Activity activity) {
this.sharedPreferences = activity.getSharedPreferences(SHARED_TUTO, Context.MODE_PRIVATE);
this.container = new FrameLayout(activity);
this.tutoView = new TutoView(activity);
Window window = activity.getWindow();
if (window != null) {
ViewGroup decorView = (ViewGroup) window.getDecorView();
if (decorView != null) {
ViewGroup content = decorView.findViewById(android.R.id.content);
if (content != null) {
content.addView(container, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
this.container.addView(tutoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
if (android.os.Build.VERSION.SDK_INT >= 16) {
View inflatedLayout = content.getChildAt(0);
this.fitsSystemWindows = inflatedLayout != null && inflatedLayout.getFitsSystemWindows();
}
}
}
}
this.container.setVisibility(View.GONE);
ViewCompat.setAlpha(container, 0f);
}
@TargetApi(21)
private boolean applySystemWindowInsets21(WindowInsetsCompat insets) {
boolean consumed = false;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (!child.getFitsSystemWindows()) {
continue;
}
Rect childInsets = new Rect(
insets.getSystemWindowInsetLeft(),
insets.getSystemWindowInsetTop(),
insets.getSystemWindowInsetRight(),
insets.getSystemWindowInsetBottom());
computeInsetsWithGravity(child, childInsets);
ViewCompat.dispatchApplyWindowInsets(child, insets.replaceSystemWindowInsets(childInsets));
consumed = true;
}
return consumed;
}
/**
* 解决安卓4.4和EMUI3.1导航栏与状态栏的问题,以及系统属性fitsSystemWindows的坑
*/
private void solveNavigation() {
for (int i = 0, count = mContentView.getChildCount(); i < count; i++) {
View childView = mContentView.getChildAt(i);
if (childView instanceof ViewGroup) {
if (childView instanceof DrawerLayout) {
View childAt1 = ((DrawerLayout) childView).getChildAt(0);
if (childAt1 != null) {
mBarParams.systemWindows = childAt1.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
} else {
mBarParams.systemWindows = childView.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
}
}
// 解决android4.4有导航栏的情况下,activity底部被导航栏遮挡的问题
if (mConfig.hasNavigtionBar() && !mBarParams.fullScreenTemp && !mBarParams.fullScreen) {
if (mConfig.isNavigationAtBottom()) { //判断导航栏是否在底部
if (!mBarParams.isSupportActionBar) { //判断是否支持actionBar
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(),
0, mConfig.getNavigationBarHeight()); //有导航栏,获得rootView的根节点,然后设置距离底部的padding值为导航栏的高度值
else
mContentView.setPadding(0, 0, 0, mConfig.getNavigationBarHeight());
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
} else {
//支持有actionBar的界面
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable)
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, mConfig.getNavigationBarHeight());
else
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, 0);
}
} else {
if (!mBarParams.isSupportActionBar) {
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(),
mConfig.getNavigationBarWidth(), 0); //不在底部,设置距离右边的padding值为导航栏的宽度值
else
mContentView.setPadding(0, 0, mConfig.getNavigationBarWidth(), 0);
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
} else {
//支持有actionBar的界面
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable)
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, mConfig.getNavigationBarWidth(), 0);
else
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, 0);
}
}
} else {
if (!mBarParams.isSupportActionBar) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
} else {
//支持有actionBar的界面
mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight() + 10, 0, 0);
}
}
}
@SuppressLint("NewApi")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
if (Build.VERSION.SDK_INT < 21)
{
inLayout = true;
if (heightSize == AndroidUtilities.displaySize.y + AndroidUtilities.statusBarHeight)
{
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
{
setPadding(0, AndroidUtilities.statusBarHeight, 0, 0);
}
heightSize = AndroidUtilities.displaySize.y;
}
else
{
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
{
setPadding(0, 0, 0, 0);
}
}
inLayout = false;
}
final boolean applyInsets = lastInsets != null && Build.VERSION.SDK_INT >= 21;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++)
{
final View child = getChildAt(i);
if (child.getVisibility() == GONE)
{
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets)
{
if (child.getFitsSystemWindows())
{
dispatchChildInsets(child, lastInsets, lp.gravity);
}
else if (child.getTag() == null)
{
applyMarginInsets(lp, lastInsets, lp.gravity, Build.VERSION.SDK_INT >= 21);
}
}
if (drawerLayout != child)
{
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
child.measure(contentWidthSpec, contentHeightSpec);
}
else
{
child.setPadding(0, 0, 0, 0);
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, minDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height);
child.measure(drawerWidthSpec, drawerHeightSpec);
}
}
}
/**
* 解决安卓4.4和EMUI3.1导航栏与状态栏的问题,以及系统属性fitsSystemWindows的坑
*/
private void solveNavigation() {
for (int i = 0, count = mContentView.getChildCount(); i < count; i++) {
View childView = mContentView.getChildAt(i);
if (childView instanceof ViewGroup) {
if (childView instanceof DrawerLayout) {
View childAt1 = ((DrawerLayout) childView).getChildAt(0);
if (childAt1 != null) {
mBarParams.systemWindows = childAt1.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
} else {
mBarParams.systemWindows = childView.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
}
}
// 解决android4.4有导航栏的情况下,activity底部被导航栏遮挡的问题
if (mConfig.hasNavigtionBar() && !mBarParams.fullScreenTemp && !mBarParams.fullScreen) {
if (mConfig.isNavigationAtBottom()) { //判断导航栏是否在底部
if (!mBarParams.isSupportActionBar) { //判断是否支持actionBar
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(),
0, mConfig.getNavigationBarHeight()); //有导航栏,获得rootView的根节点,然后设置距离底部的padding值为导航栏的高度值
else
mContentView.setPadding(0, 0, 0, mConfig.getNavigationBarHeight());
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
} else {
//支持有actionBar的界面
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable)
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, mConfig.getNavigationBarHeight());
else
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, 0);
}
} else {
if (!mBarParams.isSupportActionBar) {
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(),
mConfig.getNavigationBarWidth(), 0); //不在底部,设置距离右边的padding值为导航栏的宽度值
else
mContentView.setPadding(0, 0, mConfig.getNavigationBarWidth(), 0);
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
} else {
//支持有actionBar的界面
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable)
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, mConfig.getNavigationBarWidth(), 0);
else
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, 0);
}
}
} else {
if (!mBarParams.isSupportActionBar) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
} else {
//支持有actionBar的界面
mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight() + 10, 0, 0);
}
}
}
@SuppressLint("NewApi")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
if (Build.VERSION.SDK_INT < 21)
{
inLayout = true;
if (heightSize == AndroidUtilities.displaySize.y + AndroidUtilities.statusBarHeight)
{
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
{
setPadding(0, AndroidUtilities.statusBarHeight, 0, 0);
}
heightSize = AndroidUtilities.displaySize.y;
}
else
{
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams)
{
setPadding(0, 0, 0, 0);
}
}
inLayout = false;
}
final boolean applyInsets = lastInsets != null && Build.VERSION.SDK_INT >= 21;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++)
{
final View child = getChildAt(i);
if (child.getVisibility() == GONE)
{
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets)
{
if (child.getFitsSystemWindows())
{
dispatchChildInsets(child, lastInsets, lp.gravity);
}
else if (child.getTag() == null)
{
applyMarginInsets(lp, lastInsets, lp.gravity, Build.VERSION.SDK_INT >= 21);
}
}
if (drawerLayout != child)
{
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
child.measure(contentWidthSpec, contentHeightSpec);
}
else
{
child.setPadding(0, 0, 0, 0);
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, minDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height);
child.measure(drawerWidthSpec, drawerHeightSpec);
}
}
}
/**
* 解决安卓4.4和EMUI3.1导航栏与状态栏的问题,以及系统属性fitsSystemWindows的坑
*/
private void solveNavigation() {
for (int i = 0, count = mContentView.getChildCount(); i < count; i++) {
View childView = mContentView.getChildAt(i);
if (childView instanceof ViewGroup) {
if (childView instanceof DrawerLayout) {
View childAt1 = ((DrawerLayout) childView).getChildAt(0);
if (childAt1 != null) {
mBarParams.systemWindows = childAt1.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
} else {
mBarParams.systemWindows = childView.getFitsSystemWindows();
if (mBarParams.systemWindows) {
mContentView.setPadding(0, 0, 0, 0);
return;
}
}
}
}
// 解决android4.4有导航栏的情况下,activity底部被导航栏遮挡的问题
if (mConfig.hasNavigtionBar() && !mBarParams.fullScreenTemp && !mBarParams.fullScreen) {
if (mConfig.isNavigationAtBottom()) { //判断导航栏是否在底部
if (!mBarParams.isSupportActionBar) { //判断是否支持actionBar
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(),
0, mConfig.getNavigationBarHeight()); //有导航栏,获得rootView的根节点,然后设置距离底部的padding值为导航栏的高度值
else
mContentView.setPadding(0, 0, 0, mConfig.getNavigationBarHeight());
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
} else {
//支持有actionBar的界面
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable)
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, mConfig.getNavigationBarHeight());
else
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, 0);
}
} else {
if (!mBarParams.isSupportActionBar) {
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(),
mConfig.getNavigationBarWidth(), 0); //不在底部,设置距离右边的padding值为导航栏的宽度值
else
mContentView.setPadding(0, 0, mConfig.getNavigationBarWidth(), 0);
} else {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
}
} else {
//支持有actionBar的界面
if (mBarParams.navigationBarEnable && mBarParams.navigationBarWithKitkatEnable)
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, mConfig.getNavigationBarWidth(), 0);
else
mContentView.setPadding(0, mConfig.getStatusBarHeight() +
mConfig.getActionBarHeight() + 10, 0, 0);
}
}
} else {
if (!mBarParams.isSupportActionBar) {
if (mBarParams.fits)
mContentView.setPadding(0, mConfig.getStatusBarHeight(), 0, 0);
else
mContentView.setPadding(0, 0, 0, 0);
} else {
//支持有actionBar的界面
mContentView.setPadding(0, mConfig.getStatusBarHeight() + mConfig.getActionBarHeight() + 10, 0, 0);
}
}
}
public static boolean getFitsSystemWindows(View view) {
return view.getFitsSystemWindows();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY || heightMode != MeasureSpec.EXACTLY) {
if (isInEditMode()) {
// Don't crash the layout editor. Consume all of the space if specified
// or pick a magic number from thin air otherwise.
// TODO Better communication with tools of this bogus state.
// It will crash on a real device.
if (widthMode == MeasureSpec.AT_MOST) {
widthMode = MeasureSpec.EXACTLY;
} else if (widthMode == MeasureSpec.UNSPECIFIED) {
widthMode = MeasureSpec.EXACTLY;
widthSize = 300;
}
if (heightMode == MeasureSpec.AT_MOST) {
heightMode = MeasureSpec.EXACTLY;
} else if (heightMode == MeasureSpec.UNSPECIFIED) {
heightMode = MeasureSpec.EXACTLY;
heightSize = 300;
}
} else {
throw new IllegalArgumentException(
"DrawerLayout must be measured with MeasureSpec.EXACTLY.");
}
}
setMeasuredDimension(widthSize, heightSize);
final boolean applyInsets = mLastInsets != null && getFitsSystemWindows();
final int layoutDirection = getLayoutDirection(this);
// Only one drawer is permitted along each vertical edge (left / right). These two booleans
// are tracking the presence of the edge drawers.
boolean hasDrawerOnLeftEdge = false;
boolean hasDrawerOnRightEdge = false;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets) {
final int cgrav = Gravity.getAbsoluteGravity(lp.gravity, layoutDirection);
if (child.getFitsSystemWindows()) {
IMPL.dispatchChildInsets(child, mLastInsets, cgrav);
} else {
IMPL.applyMarginInsets(lp, mLastInsets, cgrav);
}
}
if (isContentView(child)) {
// Content views get measured at exactly the layout's size.
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(
widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(
heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
child.measure(contentWidthSpec, contentHeightSpec);
} else if (isDrawerView(child)) {
final int childGravity = getDrawerViewAbsoluteGravity(child) & Gravity.HORIZONTAL_GRAVITY_MASK;
// Note that the isDrawerView check guarantees that childGravity here is either
// LEFT or RIGHT
boolean isLeftEdgeDrawer = (childGravity == Gravity.LEFT);
if ((isLeftEdgeDrawer && hasDrawerOnLeftEdge)
|| (!isLeftEdgeDrawer && hasDrawerOnRightEdge)) {
throw new IllegalStateException("Child drawer has absolute gravity "
+ gravityToString(childGravity) + " but this " + TAG + " already has a "
+ "drawer view along that edge");
}
if (isLeftEdgeDrawer) {
hasDrawerOnLeftEdge = true;
} else {
hasDrawerOnRightEdge = true;
}
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec,
mMinDrawerMargin + lp.leftMargin + lp.rightMargin,
lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec,
lp.topMargin + lp.bottomMargin,
lp.height);
child.measure(drawerWidthSpec, drawerHeightSpec);
} else {
throw new IllegalStateException("Child " + child + " at index " + i
+ " does not have a valid layout_gravity - must be Gravity.LEFT, "
+ "Gravity.RIGHT or Gravity.NO_GRAVITY");
}
}
}
@TargetApi(19)
private boolean applySystemWindowInsets19(Rect insets) {
boolean consumed = false;
for (int i = 0; i < getChildCount(); i++) {
View child = getChildAt(i);
if (!child.getFitsSystemWindows()) {
continue;
}
Rect childInsets = new Rect(insets);
computeInsetsWithGravity(child, childInsets);
child.setPadding(childInsets.left, childInsets.top, childInsets.right, childInsets.bottom);
consumed = true;
}
return consumed;
}
public static boolean getFitsSystemWindows(View view) {
return view.getFitsSystemWindows();
}
@SuppressLint("NewApi")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
if (Build.VERSION.SDK_INT < 21) {
inLayout = true;
if (heightSize == AndroidUtilities.displaySize.y + AndroidUtilities.statusBarHeight) {
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
setPadding(0, AndroidUtilities.statusBarHeight, 0, 0);
}
heightSize = AndroidUtilities.displaySize.y;
} else {
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
setPadding(0, 0, 0, 0);
}
}
inLayout = false;
} else {
int newSize = heightSize - AndroidUtilities.statusBarHeight;
if (newSize > 0 && newSize < 4096) {
AndroidUtilities.displaySize.y = newSize;
}
}
final boolean applyInsets = lastInsets != null && Build.VERSION.SDK_INT >= 21;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets) {
if (child.getFitsSystemWindows()) {
dispatchChildInsets(child, lastInsets, lp.gravity);
} else if (child.getTag() == null) {
applyMarginInsets(lp, lastInsets, lp.gravity, Build.VERSION.SDK_INT >= 21);
}
}
if (drawerLayout != child) {
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
child.measure(contentWidthSpec, contentHeightSpec);
} else {
child.setPadding(0, 0, 0, 0);
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, minDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height);
child.measure(drawerWidthSpec, drawerHeightSpec);
}
}
}
@SuppressLint("NewApi")
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
if (Build.VERSION.SDK_INT < 21) {
inLayout = true;
if (heightSize == AndroidUtilities.displaySize.y + AndroidUtilities.statusBarHeight) {
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
setPadding(0, AndroidUtilities.statusBarHeight, 0, 0);
}
heightSize = AndroidUtilities.displaySize.y;
} else {
if (getLayoutParams() instanceof ViewGroup.MarginLayoutParams) {
setPadding(0, 0, 0, 0);
}
}
inLayout = false;
} else {
int newSize = heightSize - AndroidUtilities.statusBarHeight;
if (newSize > 0 && newSize < 4096) {
AndroidUtilities.displaySize.y = newSize;
}
}
final boolean applyInsets = lastInsets != null && Build.VERSION.SDK_INT >= 21;
final int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
if (child.getVisibility() == GONE) {
continue;
}
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
if (applyInsets) {
if (child.getFitsSystemWindows()) {
dispatchChildInsets(child, lastInsets, lp.gravity);
} else if (child.getTag() == null) {
applyMarginInsets(lp, lastInsets, lp.gravity, Build.VERSION.SDK_INT >= 21);
}
}
if (drawerLayout != child) {
final int contentWidthSpec = MeasureSpec.makeMeasureSpec(widthSize - lp.leftMargin - lp.rightMargin, MeasureSpec.EXACTLY);
final int contentHeightSpec = MeasureSpec.makeMeasureSpec(heightSize - lp.topMargin - lp.bottomMargin, MeasureSpec.EXACTLY);
child.measure(contentWidthSpec, contentHeightSpec);
} else {
child.setPadding(0, 0, 0, 0);
final int drawerWidthSpec = getChildMeasureSpec(widthMeasureSpec, minDrawerMargin + lp.leftMargin + lp.rightMargin, lp.width);
final int drawerHeightSpec = getChildMeasureSpec(heightMeasureSpec, lp.topMargin + lp.bottomMargin, lp.height);
child.measure(drawerWidthSpec, drawerHeightSpec);
}
}
}