Динамическое представление в Android
У меня есть файл кода в c#
, c#
больше похоже на Java. Есть ли способ добавить edittext
или button
или spinner
с сервера на приложение? а не из XML
или же java
(c#
.cs
файл). Я хочу, чтобы когда мой веб-администратор оставался удаленно, чтобы обновить приложение, и если он хочет еще несколько spinners
, edittext
или же buttons
тогда он сможет сделать это без того, чтобы я на самом деле перекодировал приложение с моей стороны. это мое .cs
файл до сих пор. Но JAVA (ANDROID) в порядке со мной.
using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using Android.Content.Res;
using Android.Util;
namespace EditDynamics
{
[Activity(Label = "EditDynamics", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
RelativeLayout rl;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
// SetContentView(Resource.Layout.Main);
// Get our button from the layout resource,
// and attach an event to it
rl = new RelativeLayout(this);
rl.SetBackgroundColor(Android.Graphics.Color.CadetBlue);
//rl.CanScrollVertically();
Spinner sp = new Spinner(this);
EditText et1 = new EditText(this);
EditText et2 = new EditText(this);
EditText et3 = new EditText(this);
EditText et4 = new EditText(this);
EditText et5 = new EditText(this);
TextView tv1 = new TextView(this);
Button bt1 = new Button(this);
tv1.SetBackgroundColor(Android.Graphics.Color.DarkRed);
et1.SetTextColor(Android.Graphics.Color.Black);
et2.SetTextColor(Android.Graphics.Color.Black);
et3.SetTextColor(Android.Graphics.Color.Black);
et4.SetTextColor(Android.Graphics.Color.Black);
et5.SetTextColor(Android.Graphics.Color.Black);
//Set Text on Button
bt1.Text = "Send Me";
//setting Id Programatically
sp.Id = 1; et1.Id = 2; et2.Id = 3; et3.Id = 4; et4.Id = 5; tv1.Id = 6; bt1.Id = 7;
/* a layout view is typically created by declaring how
the view should be sized in relation to the parent view */
RelativeLayout.LayoutParams et1Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
// et1Params.TopMargin = 50;
RelativeLayout.LayoutParams et2Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
// et2Params.TopMargin = 100;
RelativeLayout.LayoutParams et3Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
// et3Params.TopMargin = 150;
RelativeLayout.LayoutParams et4Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
// et4Params.TopMargin = 200;
RelativeLayout.LayoutParams et5Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
// et5Params.TopMargin = 250;
RelativeLayout.LayoutParams tv1Params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MatchParent, RelativeLayout.LayoutParams.WrapContent);
// tv1Params.TopMargin = 400;
RelativeLayout.LayoutParams btParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);
//setting the properties of the layout params
btParams.LeftMargin = 100;
btParams.RightMargin = 100;
et1Params.AddRule(LayoutRules.Above);
et1Params.AddRule(LayoutRules.CenterHorizontal);
et1Params.SetMargins(0, 0, 0, 40);
et2Params.AddRule(LayoutRules.Below, et1.Id);
et2Params.AddRule(LayoutRules.CenterHorizontal);
et2Params.SetMargins(0, 0, 0, 40);
et3Params.AddRule(LayoutRules.Below, et2.Id);
et3Params.AddRule(LayoutRules.CenterHorizontal);
et3Params.SetMargins(0, 0, 0, 40);
et4Params.AddRule(LayoutRules.Below, et3.Id);
et4Params.AddRule(LayoutRules.CenterHorizontal);
et4Params.SetMargins(0, 0, 0, 40);
et5Params.AddRule(LayoutRules.Below, et4.Id);
et5Params.AddRule(LayoutRules.CenterHorizontal);
et5Params.SetMargins(0, 0, 0, 40);
tv1Params.AddRule(LayoutRules.Below, et4.Id);
tv1Params.AddRule(LayoutRules.CenterHorizontal);
tv1Params.SetMargins(0, 0, 0, 150);
btParams.AddRule(LayoutRules.Below, tv1.Id);
btParams.AddRule(LayoutRules.CenterHorizontal);
btParams.AddRule(LayoutRules.CenterVertical);
//convert de to px for edittext
Resources r = Resources;
int px = (int)TypedValue.ApplyDimension(ComplexUnitType.Dip,
200,
r.DisplayMetrics);
et1.SetWidth(px);
et2.SetWidth(px);
et3.SetWidth(px);
et4.SetWidth(px);
et5.SetWidth(px);
rl.AddView(et1, et1Params);
rl.AddView(et2, et2Params);
rl.AddView(et3, et3Params);
rl.AddView(et4, et4Params);
rl.AddView(et5, et5Params);
rl.AddView(tv1, tv1Params);
rl.AddView(bt1, btParams);
SetContentView(rl);
}
}
}