为什么重力不会对我的按钮产生影响?

时间:2016-09-30 05:18:58

标签: android xamarin

我正在使用Signature Pad,我希望使用Gravity移动我在该视图顶部添加的按钮,但没有任何反应,我尝试了几个标记,它们都看起来像相同。我还尝试在方法结束时将signature视图添加到layout,但这并没有改变任何内容。

活动:

[Activity(Label = "Authorize Buyin", ScreenOrientation = Android.Content.PM.ScreenOrientation.Landscape)]
    public class Sign : Activity
    {
        LinearLayout layout;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.sign_dialog);

            layout = FindViewById<LinearLayout>(Resource.Id.layoutSign);

            CreateSignaturePad();
        }

        private void CreateSignaturePad()
        {
            var signature = new SignaturePadView(this)
            {
                StrokeWidth = 3f,
                BackgroundColor = Android.Graphics.Color.White,
                StrokeColor = Android.Graphics.Color.Black,
            };

            layout.AddView(signature,
                new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent));

            /* Add Clear Button */
            Button btnClear = new Button(this) { Text = "Borrar" };
            signature.AddView(btnClear);



            /* Add Authorize Button */
            Button btnAuthorize = new Button(this) { Text = "Authorize" };
            btnAuthorize.Gravity = GravityFlags.Center;
            signature.AddView(btnAuthorize);                    
        }
    }

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutSign"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="2dp">
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

SignatureView继承自RelativeLayout。 signatureSignatureView

这就是我最终解决的问题:

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WrapContent,
                RelativeLayout.LayoutParams.WrapContent);
            lp.AddRule(LayoutRules.AlignParentBottom);

            Button btnAuthorize = new Button(this) { Text = "Authorize" };
            btnAuthorize.LayoutParameters = lp;
            btnAuthorize.Click += BtnAuthorize_Click;
            signature.AddView(btnAuthorize);