Last active
March 27, 2019 12:50
-
-
Save samuel22gj/fd7e9aabf770a6fdb190e3df49241abf to your computer and use it in GitHub Desktop.
Fix windowSoftInputMode=“adjustResize” not working when activity is full screen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<declare-styleable name="FitsSystemWindowsFrameLayout"> | |
<attr name="consume_system_windows"> | |
<flag name="left" value="0x0001"/> | |
<flag name="top" value="0x0002"/> | |
<flag name="right" value="0x0004"/> | |
<flag name="bottom" value="0x0008"/> | |
</attr> | |
</declare-styleable> | |
</resources> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A kind of FrameLayout which enable {@code fitsSystemWindows} and can only consume specific directions. | |
*/ | |
public class FitsSystemWindowsFrameLayout extends FrameLayout { | |
private static final String TAG = FitsSystemWindowsFrameLayout.class.getSimpleName(); | |
private static final int FLAG_CONSUME_SYSTEM_WINDOWS_LEFT = 0x0001; | |
private static final int FLAG_CONSUME_SYSTEM_WINDOWS_TOP = 0x0002; | |
private static final int FLAG_CONSUME_SYSTEM_WINDOWS_RIGHT = 0x0004; | |
private static final int FLAG_CONSUME_SYSTEM_WINDOWS_BOTTOM = 0x0008; | |
private boolean mConsumeSystemWindowInsetLeft; | |
private boolean mConsumeSystemWindowInsetTop; | |
private boolean mConsumeSystemWindowInsetRight; | |
private boolean mConsumeSystemWindowInsetBottom; | |
public FitsSystemWindowsFrameLayout(Context context) { | |
this(context, null); | |
} | |
public FitsSystemWindowsFrameLayout(Context context, AttributeSet attrs) { | |
this(context, attrs, 0); | |
} | |
public FitsSystemWindowsFrameLayout(Context context, AttributeSet attrs, int defStyle) { | |
super(context, attrs, defStyle); | |
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FitsSystemWindowsFrameLayout); | |
int consumeSystemWindowInsetFlag = typedArray.getInt(R.styleable.FitsSystemWindowsFrameLayout_consume_system_windows, 0); | |
mConsumeSystemWindowInsetLeft = (consumeSystemWindowInsetFlag & FLAG_CONSUME_SYSTEM_WINDOWS_LEFT) == FLAG_CONSUME_SYSTEM_WINDOWS_LEFT; | |
mConsumeSystemWindowInsetTop = (consumeSystemWindowInsetFlag & FLAG_CONSUME_SYSTEM_WINDOWS_TOP) == FLAG_CONSUME_SYSTEM_WINDOWS_TOP; | |
mConsumeSystemWindowInsetRight = (consumeSystemWindowInsetFlag & FLAG_CONSUME_SYSTEM_WINDOWS_RIGHT) == FLAG_CONSUME_SYSTEM_WINDOWS_RIGHT; | |
mConsumeSystemWindowInsetBottom = (consumeSystemWindowInsetFlag & FLAG_CONSUME_SYSTEM_WINDOWS_BOTTOM) == FLAG_CONSUME_SYSTEM_WINDOWS_BOTTOM; | |
typedArray.recycle(); | |
} | |
@Override | |
public WindowInsets onApplyWindowInsets(WindowInsets insets) { | |
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { | |
// Create a new WindowInsets, which only retain the directions we want to handle, | |
// and put back to super method. | |
return super.onApplyWindowInsets(insets.replaceSystemWindowInsets( | |
mConsumeSystemWindowInsetLeft ? insets.getSystemWindowInsetLeft() : 0, | |
mConsumeSystemWindowInsetTop ? insets.getSystemWindowInsetTop() : 0, | |
mConsumeSystemWindowInsetRight ? insets.getSystemWindowInsetRight() : 0, | |
mConsumeSystemWindowInsetBottom ? insets.getSystemWindowInsetBottom() : 0)); | |
} else { | |
return super.onApplyWindowInsets(insets); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment