如何使Xamarin.Forms.Editor可滚动/自动调整大小? [英] How to make Xamarin.Forms.Editor scrollable/auto resizable?
问题描述
我有一个scrollabe布局,里面有一个Editor,我想使其可以滚动或自动调整大小以适合内容.
I have an scrollabe layout with an Editor inside that I'd like to make scrollable or auto resizable to fit the contents.
我找不到方法.
我尝试了一个自定义渲染器,但是找不到如何将InputMethods设置为Control.
I tried a custom renderer but I can't find how to set InputMethods to the Control.
有什么想法吗?
推荐答案
借助本文:这修复了Android上的滚动(默认情况下,iOS可以运行).触摸编辑器内部时,它避免了父级滚动事件,仅触发了编辑器滚动.
That fixed the scrolling on Android (iOS works by default). It avoids the parent scroll event when touching inside the Editor, triggering only the Editor scroll.
首先是Android项目上的一门课:
First a class on Android project:
using Android.Views;
namespace MyApp.Droid
{
public class DroidTouchListener : Java.Lang.Object, View.IOnTouchListener
{
public bool OnTouch(View v, MotionEvent e)
{
v.Parent?.RequestDisallowInterceptTouchEvent(true);
if ((e.Action & MotionEventActions.Up) != 0 && (e.ActionMasked & MotionEventActions.Up) != 0)
{
v.Parent?.RequestDisallowInterceptTouchEvent(false);
}
return false;
}
}
}
然后在Android Custom EditorRenderer上使用它:
And then use it on the Android Custom EditorRenderer:
protected override void OnElementChanged(ElementChangedEventArgs<Editor> e)
{
base.OnElementChanged(e);
if (e.OldElement == null)
{
var nativeEditText = (global::Android.Widget.EditText)Control;
//While scrolling inside Editor stop scrolling parent view.
nativeEditText.OverScrollMode = OverScrollMode.Always;
nativeEditText.ScrollBarStyle = ScrollbarStyles.InsideInset;
nativeEditText.SetOnTouchListener(new DroidTouchListener());
//For Scrolling in Editor innner area
Control.VerticalScrollBarEnabled = true;
Control.MovementMethod = ScrollingMovementMethod.Instance;
Control.ScrollBarStyle = Android.Views.ScrollbarStyles.InsideInset;
//Force scrollbars to be displayed
Android.Content.Res.TypedArray a = Control.Context.Theme.ObtainStyledAttributes(new int[0]);
InitializeScrollbars(a);
a.Recycle();
}
}
这篇关于如何使Xamarin.Forms.Editor可滚动/自动调整大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!