问题
I have a chat which is "mixed", the editText and send button is in java / native and my chat system is in a server and I load it inside of a webview.
I would like to adjust my webview with my keyboard when the user starts typing. I used adjustPan for that and works, but now my actionbar somehow is hidden when the keyboard is open.
Here is part of my android manifest for my Activity:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
android:windowSoftInputMode="adjustPan">
</activity>
Here is my chat.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="@+id/llFooter">
</WebView>
<LinearLayout
android:id="@id/llFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<EditText
android:id="@+id/chat_message"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.9"
android:maxLines="3"
android:hint="Schreibe deine Nachricht hier..">
</EditText>
<Button
android:id="@+id/chat_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:text=">">
</Button>
</LinearLayout>
</RelativeLayout>
Note: my chat is a fragment
Anyone has an idea how can I fix it?
I already checked these links without success:
ActionBar is hiding when keyboard appears
Soft Keyboard Hiding ActionBar while using adjustPan
EDIT 1: SCREENSHOTS ADDED
AdjustPan: The actionbar is hidden but the chat webview fits with the keyboard AdjustResize: The actionbar is not hidden but the chat webview doesnt fit. Should scroll down until the last messages


回答1:
Here is the solution for this fix
android:windowSoftInputMode="adjustResize"
in Manifest File- Make a class
"CommentKeyBoardFix.Java"
and copy and paste the below code.
public class CommentKeyBoardFix
{
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private Rect contentAreaOfWindowBounds = new Rect();
public CommentKeyBoardFix(Activity activity)
{
FrameLayout content = activity.findViewById(android.R.id.content);
mChildOfContent = content.getChildAt(0);
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(this::possiblyResizeChildOfContent);
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
private void possiblyResizeChildOfContent()
{
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious)
{
int heightDifference = 0;
if (heightDifference > (usableHeightNow /4))
{
// keyboard probably just became visible
frameLayoutParams.height = usableHeightNow - heightDifference;
}
else
{
// keyboard probably just became hidden
frameLayoutParams.height = usableHeightNow;
}
mChildOfContent.layout(contentAreaOfWindowBounds.left, contentAreaOfWindowBounds.top, contentAreaOfWindowBounds.right, contentAreaOfWindowBounds.bottom);
mChildOfContent.requestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight()
{
mChildOfContent.getWindowVisibleDisplayFrame(contentAreaOfWindowBounds);
return contentAreaOfWindowBounds.height();
}
}
And then call this class in your Activity or Fragment
For Kotlin,
setContentView(R.layout.your_comments_layout)
CommentKeyBoardFix(this)
or
For Java
new CommentKeyBoardFix(this)
来源:https://stackoverflow.com/questions/25892331/keyboard-hides-actionbar-while-using-adjustpan