C# Xamarin将视图添加到布局,但在屏幕上不可见
从我的代码中,我尝试向屏幕添加一些视图(用于将来添加动态视图控件),但它在屏幕上不可见。 对于最后一行的var TextTest,它显示文本结果“0000000000000”。 要将此控件显示到布局,我需要将哪个属性设置为TextViewC# Xamarin将视图添加到布局,但在屏幕上不可见,c#,android,xamarin,android-linearlayout,textview,C#,Android,Xamarin,Android Linearlayout,Textview,从我的代码中,我尝试向屏幕添加一些视图(用于将来添加动态视图控件),但它在屏幕上不可见。 对于最后一行的var TextTest,它显示文本结果“0000000000000”。 要将此控件显示到布局,我需要将哪个属性设置为TextView [Activity()] public class LoginActivity : Activity { protected override async void OnCreate(Bundle savedInstanceState) {
[Activity()]
public class LoginActivity : Activity
{
protected override async void OnCreate(Bundle savedInstanceState)
{
ActionBar.Hide();
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Login);
var layout = FindViewById<LinearLayout>(Resource.Id.loginLayout);
var button1 = FindViewById<Button>(Resource.Id.btnCallApi);
button1.Click += (se, ev) =>
{
var param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.WrapContent);
var textView2 = new TextView(layout.Context) { Id = 123 };
param.AddRule(LayoutRules.Below, textView2.Id);
textView2.Text = "0000000000000";
textView2.SetX(300);
textView2.SetY(300);
textView2.ScaleY = 300;
textView2.ScaleX = 300;
textView2.TextSize = 30;
textView2.SetWidth(300);
textView2.SetHeight(300);
layout.AddView(textView2, param);
layout.Invalidate();
var newTx = FindViewById<TextView>(123);
var TextTest = newTx.Text;
};
}
}
[Activity()]
公共类后勤活动:活动
{
受保护的重写异步void OnCreate(Bundle savedInstanceState)
{
ActionBar.Hide();
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Login);
var布局=findviewbyd(Resource.Id.loginLayout);
var button1=findviewbyd(Resource.Id.btnCallApi);
按钮1.单击+=(se,ev)=>
{
var param=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
ViewGroup.LayoutParams.WrapContent);
var textView2=newtextview(layout.Context){Id=123};
参数AddRule(LayoutRules.Below,textView2.Id);
textView2.Text=“0000000000000”;
textView2.SetX(300);
textView2.SetY(300);
textView2.ScaleY=300;
textView2.ScaleX=300;
textView2.TextSize=30;
textView2.SetWidth(300);
textView2.设置高度(300);
layout.AddView(textView2,param);
layout.Invalidate();
var newTx=FindViewById(123);
var TextTest=newTx.Text;
};
}
}
您一次指定的参数太多,大多数都不需要
LinearLayout会自动放置其子对象,因此无需指定它们的位置
此外,缩放值为300,这实际上意味着将对象缩放300倍,即原始大小的30000%。我怀疑你是否想要它
因此,总而言之,在开始时,去掉大多数属性更改,显示文本视图,然后开始逐步应用这些参数(如果确实需要)
// var param = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
//ViewGroup.LayoutParams.WrapContent);
var textView2 = new TextView(layout.Context) { Id = 123 };
//param.AddRule(LayoutRules.Below, textView2.Id);
textView2.Text = "0000000000000";
//textView2.SetX(300);
//textView2.SetY(300);
//textView2.ScaleY = 300;
//textView2.ScaleX = 300;
//textView2.TextSize = 30;
//textView2.SetWidth(300);
//textView2.SetHeight(300);
layout.AddView(textView2/*, param*/);
//layout.Invalidate();
//var newTx = FindViewById<TextView>(123);
//var TextTest = newTx.Text;
//var param=新的RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent,
//ViewGroup.LayoutParams.WrapContent);
var textView2=newtextview(layout.Context){Id=123};
//参数AddRule(LayoutRules.Below,textView2.Id);
textView2.Text=“0000000000000”;
//textView2.SetX(300);
//textView2.SetY(300);
//textView2.ScaleY=300;
//textView2.ScaleX=300;
//textView2.TextSize=30;
//textView2.SetWidth(300);
//textView2.设置高度(300);
layout.AddView(textView2/*,param*/);
//layout.Invalidate();
//var newTx=FindViewById(123);
//var TextTest=newTx.Text;