Use C # + Xamarin to develop Android applications -- dynamically add controls,

Source: Internet
Author: User

Use C # + Xamarin to develop Android applications -- dynamically add controls,

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;        }    }


Auxiliary class:

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;        }    }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.