Android WindowsOfInputMode.AdjustResize仍在平移
我已经在几个活动中成功地实现了AdjustResize,但有一个活动仍然拒绝在显示键盘时执行任何操作,除了平移 知道我做错了什么吗 我使用的xamarin属性如下:Android WindowsOfInputMode.AdjustResize仍在平移,android,xamarin,Android,Xamarin,我已经在几个活动中成功地实现了AdjustResize,但有一个活动仍然拒绝在显示键盘时执行任何操作,除了平移 知道我做错了什么吗 我使用的xamarin属性如下: [Activity ( LaunchMode=Android.Content.PM.LaunchMode.SingleTask, Label = "Active Jobs", MainLauncher = false, Icon = "@drawable/icon", WindowSo
[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布局
<?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>
我已经将一个工作环境转换为Xamarin.Android,基于
要实现修复,只需在活动的OnCreate
方法中添加以下代码
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);
}
}
如果像我这样的人仍然对后退按钮导航的问题感到恼火,那么我的解决方法现在已经解决了这个问题。但我仍然在测试。如果没有别的办法,试试看 主要的变化是事件,以防WindowFucusChange使用后退按钮顺利工作。缺点是当编辑器聚焦/未聚焦时,事件只被调用一次。这就是使用while循环等待屏幕大小任务的原因。也许没必要
vto.GlobalFocusChange += Vto_WindowFocusChange;
private AndroidBug5497Workaround(Activity activity)
{
var content = (FrameLayout)activity.FindViewById(Android.Resource.Id.Content);
mChildOfContent = content.GetChildAt(0);
var vto = mChildOfContent.ViewTreeObserver;
vto.GlobalFocusChange += Vto_WindowFocusChange;
frameLayoutParams = (FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
}
private async void Vto_WindowFocusChange(object sender, ViewTreeObserver.GlobalFocusChangeEventArgs e)
{
if (e.NewFocus != null && e.OldFocus != null)
{
var usableHeightNow = await computeUsableHeight(mChildOfContent.RootView.Height);
int usableHeightSansKeyboard = mChildOfContent.RootView.Height;
int heightDifference = usableHeightSansKeyboard - usableHeightNow;
frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious = usableHeightNow;
}
else
{
frameLayoutParams.Height = mChildOfContent.RootView.Height;
mChildOfContent.RequestLayout();
usableHeightPrevious = mChildOfContent.RootView.Height;
}
}
private async Task<int> computeUsableHeight(int previous)
{
Rect r = new Rect();
var w = previous;
await Task.Run(async() => {
mChildOfContent.GetWindowVisibleDisplayFrame(r);
while (r.Bottom == previous)
{
await Task.Delay(150);
if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
return (r.Bottom - r.Top);
}
mChildOfContent.GetWindowVisibleDisplayFrame(r);
System.Diagnostics.Debug.WriteLine("Current screen size... " + r.Bottom);
}
return r.Bottom;
});
return r.Bottom;
}
vto.GlobalFocusChange+=vto\u WindowFocusChange;
私有AndroidBug5497解决方案(活动)
{
var content=(FrameLayout)activity.findviewbyd(Android.Resource.Id.content);
mChildOfContent=content.GetChildAt(0);
var vto=mChildOfContent.ViewTreeObserver;
vto.GlobalFocusChange+=vto_WindowFocusChange;
frameLayoutParams=(FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
}
私有异步void Vto_WindowFocusChange(对象发送方,ViewTreeObserver.GlobalFocusChangeEventArgs e)
{
if(e.NewFocus!=null&&e.OldFocus!=null)
{
var usableHeightNow=await computeusableheights(mChildOfContent.RootView.Height);
int usableheightsanskiboard=mChildOfContent.RootView.Height;
int heightDifference=usableHeightSansKeyboard-usableHeightNow;
frameLayoutParams.Height=usableHeightSansKeyboard-heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious=usableHeightNow;
}
其他的
{
frameLayoutParams.Height=mChildOfContent.RootView.Height;
mChildOfContent.RequestLayout();
usableHeightPrevious=mChildOfContent.RootView.Height;
}
}
专用异步任务ComputeUsableLight(int-previous)
{
Rect r=新的Rect();
var w=以前的;
等待任务。运行(异步()=>{
mChildOfContent.GetWindowVisibleDisplayFrame(r);
while(r.Bottom==上一个)
{
等待任务。延迟(150);
if(Build.VERSION.SdkInt
活动中是否有在运行时设置窗口软输入模式的代码?此外,如果在某个版面上设置了限制大小调整的特定高度,则您会看到与您现在看到的类似的内容。@Karakuri没有。在android清单中都没有。@Alex.F布局的高度设置为“填充父项”。在这种情况下,我通常会用有问题的布局创建一个新项目,并开始隔离问题,这通常是有效的,因为这大部分是有效的,但有时候,当您按下后退按钮时,Xamarin.Forms似乎不会重新绘制屏幕的底部。键盘下的部分显示为空白。你有这样的问题吗?这似乎只发生在Xamarin.Forms.Hey;从来没用过Xamarin,表格!我们在Xamarin的这一部分之前构建了我们的应用程序。对我们来说似乎没问题。