本文介绍了Android WindowSoftInputMode.AdjustResize 仍在平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在几个活动中成功地实现了 AdjustResize,但特别是一个活动在显示键盘时仍然拒绝做任何事情,只能平移.
I've successfully implemented AdjustResize in several acitivies but one activitie in particular still refuses to do anything but pan when the keyboard is shown.
知道我做错了什么吗?
我正在使用这样的 xamarin 属性:
I'm using the xamarin attribute like this:
[Activity (
LaunchMode=Android.Content.PM.LaunchMode.SingleTask,
Label = "Active Jobs",
MainLauncher = false,
Icon = "@drawable/icon",
WindowSoftInputMode = SoftInput.AdjustResize,
ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait)]
此活动的底层 axml 布局
The underlying axml layout for this activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px"
android:background="#FFFFFF"
android:gravity="center|top"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="40dp">
<TextView
android:text="No active works on this device."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:gravity="center"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:visibility="gone"
android:textColor="#006985"
android:textSize="30dp" />
<ListView
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:textColor="#006985"
android:textSize="30dp"
android:divider="@null"
android:dividerHeight="-0.6dp"
android:layout_gravity="top" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center|center"
android:paddingLeft="40dp"
android:paddingRight="40dp"
android:paddingTop="40dp"
android:paddingBottom="60dp"
android:layout_alignParentBottom="true"
android:visibility="gone"
android:background="#006985"
android:orientation="vertical">
<EditText
android:inputType="textNoSuggestions"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#FFFFFF"
android:textColor="#006985"
android:textSize="30dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:hint="Find..." />
<Button
android:text="Add New Task"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="@drawable/buttonHollowWhite"
android:layout_centerHorizontal="true"
android:textColor="#FFFFFF"
android:textSize="30dp"
android:layout_marginTop="40dp"
android:layout_gravity="center" />
</LinearLayout>
</RelativeLayout>
推荐答案
我已根据此 StackOverflow 答案转换了一个适用于 Xamarin.Android 的变通方法.
I have converted a work-around to work for Xamarin.Android, based on this StackOverflow answer.
要实施修复,只需在您的活动的 OnCreate
方法中添加以下代码.
To implement the fix simply add the following code within your activity's OnCreate
method.
AndroidBug5497WorkaroundForXamarinAndroid.assistActivity (this);
完整的课程是:
public class AndroidBug5497WorkaroundForXamarinAndroid {
// For more information, see https://code.google.com/p/android/issues/detail?id=5497
// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.
// CREDIT TO Joseph Johnson (https://stackoverflow.com/users/341631/joseph-johnson) for publishing the original Android solution on stackoverflow.com
public static void assistActivity (Activity activity) {
new AndroidBug5497WorkaroundForXamarinAndroid(activity);
}
private View mChildOfContent;
private int usableHeightPrevious;
private FrameLayout.LayoutParams frameLayoutParams;
private AndroidBug5497WorkaroundForXamarinAndroid(Activity activity) {
FrameLayout content = (FrameLayout) activity.FindViewById(Android.Resource.Id.Content);
mChildOfContent = content.GetChildAt(0);
ViewTreeObserver vto = mChildOfContent.ViewTreeObserver;
vto.GlobalLayout += (object sender, EventArgs e) => {
possiblyResizeChildOfContent();
};
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.LayoutParameters;
}
private void possiblyResizeChildOfContent() {
int usableHeightNow = computeUsableHeight();
if (usableHeightNow != usableHeightPrevious) {
int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
if (heightDifference > (usableHeightSansKeyboard/4)) {
// keyboard probably just became visible
frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
} else {
// keyboard probably just became hidden
frameLayoutParams.Height = usableHeightSansKeyboard;
}
mChildOfContent.RequestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight() {
Rect r = new Rect ();
mChildOfContent.GetWindowVisibleDisplayFrame(r);
return (r.Bottom - r.Top);
}
}
这篇关于Android WindowSoftInputMode.AdjustResize 仍在平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!