xamarin.forms android - 线性布局未按预期显示

时间:2017-01-20 19:36:56

标签: xamarin.android xamarin.forms android-linearlayout

我有自定义键盘的代码。

它的xamarin.forms for android。

我希望键盘从页面底部显示,并在需要时提高输入。

结果是键盘显示在页面顶部,如果条目位于顶部,则覆盖条目。

public class CustomEntryRenderer : EntryRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Entry> e)
    {
        base.OnElementChanged(e);

        if (Control == null)
            return;

        Android.InputMethodServices.Keyboard numericKeyboard = new Android.InputMethodServices.Keyboard(Control.Context, Resource.Xml.keyboard2);
        CustomKeyboardView numericKeyboardView = new CustomKeyboardView(Control.Context, null);
        numericKeyboardView.Id = Control.Id;
        numericKeyboardView.Keyboard = numericKeyboard;
        numericKeyboardView.Visibility = ViewStates.Gone;
        numericKeyboardView.PreviewEnabled = false;

        /////////////////////////////////////////////////
        // THIS IS THE LAYOUT CREATION
        ////////////////////////////////////////////////
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent); // maybe WrapContent on all
        lp.Gravity = GravityFlags.Bottom;
        lp.BottomMargin = 0;

        /////////////////////////////////////////////////

        Activity activity = this.Context as Activity;

        activity.AddContentView(numericKeyboardView, lp);

        Control.Touch += (sender, ex2) => 
        {
            if (numericKeyboardView.Visibility == ViewStates.Gone)
            {
                //Xamarin.Forms.Animation animation = Android.Views.Animations.AnimationUtils.LoadAnimation(
                Android.Views.Animations.Animation animation = Android.Views.Animations.AnimationUtils.LoadAnimation(
                this.Context,
                    Resource.Animation.slide_in_bottom
                );
                numericKeyboardView.ShowWithAnimation(animation);
                numericKeyboardView.Visibility = ViewStates.Visible;
            }

            ex2.Handled = true;
        };                       
    }
} 

1 个答案:

答案 0 :(得分:0)

  

我希望键盘从页面底部显示,并在需要时提高输入。

您需要使用RelativeLayout包裹numericKeyboardView并设置AlignParentBottom规则。

OnElementChanged方法中,添加以下代码:

//Create RelativeLayout for Keyboard
Android.Widget.RelativeLayout rl = new Android.Widget.RelativeLayout(this.Context);
//Create the LayoutParams for Keyboard
Android.Widget.RelativeLayout.LayoutParams rlp = new Android.Widget.RelativeLayout.LayoutParams(LayoutParams.FillParent, LayoutParams.WrapContent);
//set the AlignParentBottom rule
rlp.AddRule(LayoutRules.AlignParentBottom);
//set the LayoutParams to Keyboard
numericKeyboardView.LayoutParameters = rlp;
//add the keyboard to RelativeLayout
rl.AddView(numericKeyboardView);
//get current activity
Activity activity = this.Context as Activity;
//Add the Layout View to activity
activity.AddContentView(rl,rlp);