Android Adjust resize在API 22中无法使用Xamarin.Forms上的AppCompat
我在Android上有一个Xamarin.Forms项目,我想使用“AdjustResize”作为处理软键盘的默认方法。我通过在Android Adjust resize在API 22中无法使用Xamarin.Forms上的AppCompat,android,xamarin,android-appcompat,xamarin-forms,Android,Xamarin,Android Appcompat,Xamarin Forms,我在Android上有一个Xamarin.Forms项目,我想使用“AdjustResize”作为处理软键盘的默认方法。我通过在OnCreate中将其设置为: [Activity(Label = "TestWhite.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public cl
OnCreate
中将其设置为:
[Activity(Label = "TestWhite.Droid", Icon = "@drawable/icon", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : FormsAppCompatActivity
{
protected override void OnCreate(Bundle bundle)
{
FormsAppCompatActivity.ToolbarResource = Resource.Layout.toolbar;
FormsAppCompatActivity.TabLayoutResource = Resource.Layout.tabs;
base.OnCreate(bundle);
global::Xamarin.Forms.Forms.Init(this, bundle);
Window.SetSoftInputMode(SoftInput.AdjustResize);
LoadApplication(new App());
}
}
由于我已更新到AppCompat
,AdjustResize
不适用于API 22,但仍适用于API 19
如果应用程序只包含一个页面,页面底部包含滚动视图和编辑器,则可以非常轻松地测试此问题:
public MainPage()
{
InitializePage();
}
void InitializePage()
{
var editor = new Editor
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 200,
};
var tallLayout = new StackLayout
{
HorizontalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
HeightRequest = 450,
BackgroundColor = Color.Lime,
};
var layout = new StackLayout
{
Orientation = StackOrientation.Vertical,
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Children = { tallLayout, editor },
};
var scrollView = new ScrollView
{
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
};
scrollView.Content = layout;
Content = scrollView;
}
使用API 19,当用户单击
编辑器时,视图会向上推,以便用户可以看到他在写什么。相反,使用API 22时,键盘只显示在视图上,隐藏了编辑器
。关于这种行为背后的原因有什么想法吗?在MainActivity的OnCreate方法中,您应该将输入模式设置为AdjustResize。然后,为了处理报告的android bug,您可以使用下面的实用程序类(最初由@user1658602从java代码转换而来)。快乐编码
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
Window.SetSoftInputMode(SoftInput.AdjustResize);
AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
LoadApplication(new App());
}
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 (http://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 Android.Views.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;
frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight()
{
Rect r = new Rect();
mChildOfContent.GetWindowVisibleDisplayFrame(r);
if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
return (r.Bottom - r.Top);
}
return r.Bottom;
}
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(这个,bundle);
Window.SetSoftInputMode(软输入.调整大小);
AndroidBug5497 Xamariandroid.辅助性的解决方案(本);
ToolbarResource=Resource.Layout.toolbar;
TabLayoutResource=Resource.Layout.tabs;
加载应用程序(新应用程序());
}
公共类AndroidBug5497 XamarinAndroid的解决方法
{
//有关详细信息,请参阅https://code.google.com/p/android/issues/detail?id=5497
//要使用该类,只需对已设置其内容视图的活动调用assistativity()。
//归功于约瑟夫·约翰逊(http://stackoverflow.com/users/341631/joseph-johnson)用于在stackoverflow.com上发布原始Android解决方案
公共静态无效辅助性(活动)
{
新的AndroidBug5497 Xamarinandroid解决方案(活动);
}
私有Android.Views.View mChildOfContent;
私人使用高度;
私有FrameLayout.LayoutParams frameLayoutParams;
私有AndroidBug5497 Xamariandroid的变通方法(活动)
{
FrameLayout content=(FrameLayout)activity.findviewbyd(Android.Resource.Id.content);
mChildOfContent=content.GetChildAt(0);
ViewTreeObserver vto=mChildOfContent.ViewTreeObserver;
vto.GlobalLayout+=(对象发送方,事件参数e)=>{
可能ResizeChildOfContent();
};
frameLayoutParams=(FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
}
private void possiblyResizeChildOfContent()
{
int usableHeightNow=computeusableheightnow();
如果(usableHeightNow!=usableHeightPrevious)
{
int usableheightsanskiboard=mChildOfContent.RootView.Height;
int heightDifference=usableHeightSansKeyboard-usableHeightNow;
frameLayoutParams.Height=usableHeightSansKeyboard-heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious=usableHeightNow;
}
}
私有int ComputersAbleLight()
{
Rect r=新的Rect();
mChildOfContent.GetWindowVisibleDisplayFrame(r);
if(Build.VERSION.SdkInt
在MainActivity的OnCreate方法中,应将输入模式设置为AdjustResize。然后,为了处理报告的android bug,您可以使用下面的实用程序类(最初由@user1658602从java代码转换而来)。快乐编码
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(this, bundle);
Window.SetSoftInputMode(SoftInput.AdjustResize);
AndroidBug5497WorkaroundForXamarinAndroid.assistActivity(this);
ToolbarResource = Resource.Layout.toolbar;
TabLayoutResource = Resource.Layout.tabs;
LoadApplication(new App());
}
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 (http://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 Android.Views.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;
frameLayoutParams.Height = usableHeightSansKeyboard - heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious = usableHeightNow;
}
}
private int computeUsableHeight()
{
Rect r = new Rect();
mChildOfContent.GetWindowVisibleDisplayFrame(r);
if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
{
return (r.Bottom - r.Top);
}
return r.Bottom;
}
}
protectedoverride void OnCreate(捆绑包)
{
base.OnCreate(bundle);
Xamarin.Forms.Forms.Init(这个,bundle);
Window.SetSoftInputMode(软输入.调整大小);
AndroidBug5497 Xamariandroid.辅助性的解决方案(本);
ToolbarResource=Resource.Layout.toolbar;
TabLayoutResource=Resource.Layout.tabs;
加载应用程序(新应用程序());
}
公共类AndroidBug5497 XamarinAndroid的解决方法
{
//有关详细信息,请参阅https://code.google.com/p/android/issues/detail?id=5497
//要使用该类,只需对已设置其内容视图的活动调用assistativity()。
//归功于约瑟夫·约翰逊(http://stackoverflow.com/users/341631/joseph-johnson)用于在stackoverflow.com上发布原始Android解决方案
公共静态无效辅助性(活动)
{
新的AndroidBug5497 Xamarinandroid解决方案(活动);
}
私有Android.Views.View mChildOfContent;
私人使用高度;
私有FrameLayout.LayoutParams frameLayoutParams;
私有AndroidBug5497 Xamariandroid的变通方法(活动)
{
FrameLayout content=(FrameLayout)activity.findviewbyd(Android.Resource.Id.content);
mChildOfContent=content.GetChildAt(0);
ViewTreeObserver vto=mChildOfContent.ViewTreeObserver;
vto.GlobalLayout+=(对象发送方,事件参数e)=>{
可能ResizeChildOfContent();
};
frameLayoutParams=(FrameLayout.LayoutParams)mChildOfContent.LayoutParameters;
}
private void possiblyResizeChildOfContent()
{
int usableHeightNow=computeusableheightnow();
如果(usableHeightNow!=usableHeightPrevious)
{
int usableheightsanskiboard=mChildOfContent.RootView.Height;
int heightDifference=usableHeightSansKeyboard-usableHeightNow;
frameLayoutParams.Height=usableHeightSansKeyboard-heightDifference;
mChildOfContent.RequestLayout();
usableHeightPrevious=usableHeightNow;
}
}
私有int ComputersAbleLight()
{
Rect r=新的Rect();
mChildOfContent.GetWindowVisibleDisplayFrame(r);
if(Build.VERSION.SdkInt