使用C# + Xamarin开发Android应用-- 动态添加控件

黄舟
发布: 2017-03-01 10:30:45
原创
3467 人浏览过

使用C# + Xamarin开发Android应用-- 动态添加控件

public class MyOrders : Activity
    {

        private DynamicControl _dynaControl;

        public MyOrders()
        {
            _dynaControl = new DynamicControl();
        }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            try
            {
                SetContentView(Resource.Layout.MyOrders);

                var layout = (TableLayout)FindViewById(Resource.Id.myorders_layout);
                var header = AddHeader();
                layout.AddView(header);

                layout.AddView(AddPendingServiceItem("Service 1"));
                layout.AddView(AddPendingServiceItem("Service 2"));
                layout.AddView(AddPendingServiceItem("Service 3"));

                layout.AddView(AddConfirmedServiceItem("Service 4"));
                layout.AddView(AddConfirmedServiceItem("Service 5"));
                layout.AddView(AddConfirmedServiceItem("Service 6"));
                layout.AddView(AddConfirmedServiceItem("Service 7"));
            }
            catch (Exception ex)
            {
                this.ShowAlert(ex.Message);
            }
        }

        public View AddHeader()
        {
            var header = new TableRow(this);
            header.LayoutParameters = new TableLayout.LayoutParams(TableLayout.LayoutParams.FillParent,
            TableLayout.LayoutParams.WrapContent);
            header.AddView(_dynaControl.NewText(this, "Service"));
            header.AddView(_dynaControl.NewText(this, "Status"));
            header.AddView(_dynaControl.NewText(this, "Created At"));
            header.AddView(_dynaControl.NewText(this, "Action"));
            return header;
        }

        public View AddPendingServiceItem(string serviceName)
        {
            var tableRow = new TableRow(this);
            tableRow.LayoutParameters = new TableLayout.LayoutParams(TableLayout.LayoutParams.FillParent,
                TableLayout.LayoutParams.WrapContent);

            tableRow.AddView(_dynaControl.NewText(this, string.Format("{0}  ", serviceName)));
            tableRow.AddView(_dynaControl.NewText(this, string.Format("{0}  ", "Pending"), Color.OrangeRed));
            tableRow.AddView(_dynaControl.NewText(this, string.Format("{0}  ", "2015-5-19 \r\n14:20:33")));
            tableRow.AddView(_dynaControl.AddButton(this, "Details"));
            return tableRow;
        }

        public View AddConfirmedServiceItem(string serviceName)
        {
            var tableRow = new TableRow(this);
            tableRow.AddView(_dynaControl.NewText(this, string.Format("{0}  ", serviceName)));
            tableRow.AddView(_dynaControl.NewText(this, string.Format("{0}  ", "Confirmed"), Color.DarkGreen));
            tableRow.AddView(_dynaControl.NewText(this, string.Format("{0}", "2015-5-19 \r\n 14:30:43")));
            tableRow.AddView(_dynaControl.AddButton(this, "Rebook"));

            tableRow.LayoutParameters = new TableLayout.LayoutParams(TableLayout.LayoutParams.FillParent,
                TableLayout.LayoutParams.WrapContent);

            return tableRow;
        }
    }
登录后复制

杈呭姪绫伙細

public class DynamicControl
    {
        public Button AddButton(Context context, string text, EventHandler clickAction = null)
        {
            var btn = new Button(context);
            btn.SetText(text, TextView.BufferType.Normal);
            if (clickAction != null)
            {
                btn.Click += clickAction;
            }
            
            return btn;
        }

        public TextView NewText(Context context, string str, Color? color = null)
        {
            var text = new TextView(context);
            if (color.HasValue)
            {
                text.SetTextColor(color.Value);
            }

            text.SetText(str, TextView.BufferType.Normal);

            return text;
        }
    }
登录后复制

以上就是使用C# + Xamarin开发Android应用-- 动态添加控件的内容,更多相关内容请关注PHP中文网(www.php.cn)!


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板