C# 如何在RelativeLayout中动态对齐Xamarin Mono for Android中的视图
在我决定问这个问题之前我搜索了很多。。。 我试图实现的是布局的动态创建,基本上是包含一些内容的行。。。 我将模板设计为axml布局。这是:C# 如何在RelativeLayout中动态对齐Xamarin Mono for Android中的视图,c#,android,mono,android-relativelayout,xamarin,C#,Android,Mono,Android Relativelayout,Xamarin,在我决定问这个问题之前我搜索了很多。。。 我试图实现的是布局的动态创建,基本上是包含一些内容的行。。。 我将模板设计为axml布局。这是: <RelativeLayout android:minWidth="25px" android:minHeight="25px" android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/relativeLayout1"> &l
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/relativeLayout1">
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_height="91dp"
android:id="@+id/imageView1"
android:layout_width="68.524dp" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageView1"
android:id="@+id/textView1"
android:layout_marginLeft="5" />
<TextView
android:text="Год: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/imageView1"
android:id="@+id/textView2"
android:layout_below="@id/textView1"
android:layout_marginLeft="5" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textView2"
android:id="@+id/textView3"
android:layout_below="@id/textView1" />
<TextView
android:text="Жанр: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView3"
android:id="@+id/textView4"
android:layout_toRightOf="@id/imageView1"
android:layout_marginLeft="5" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView3"
android:id="@+id/textView5"
android:layout_toRightOf="@id/textView4" />
<TextView
android:text="Оценка: "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:id="@+id/textView6"
android:layout_toRightOf="@id/imageView1"
android:layout_marginLeft="5" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:id="@+id/textView7"
android:layout_toRightOf="@id/textView6" />
<TextView
android:text="Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView5"
android:id="@+id/textView8"
android:layout_toRightOf="@id/textView7"
android:layout_marginLeft="5" />
我试图在代码中动态创建它,但所有元素都在彼此之上。。。
代码如下:
private void _buildUI(AnimeList anList)
{
var i = 1;
RelativeLayout.LayoutParams textParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent, RelativeLayout.LayoutParams.WrapContent);
foreach (var anime in anList)
{
RelativeLayout wrapper = new RelativeLayout(this);
wrapper.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent, ViewGroup.LayoutParams.WrapContent);
ImageView img = new ImageView(this);
img.LayoutParameters = new ViewGroup.LayoutParams(68, 90);
img.Id = i;
img.SetImageURI(Android.Net.Uri.Parse(anime.Image.Url));
wrapper.AddView(img);
TextView title = new TextView(this);
textParams.SetMargins(5, 0, 0, 0);
textParams.AddRule(LayoutRules.RightOf, img.Id);
title.LayoutParameters = textParams;
i++;
title.Id = i;
title.Text = anime.Title;
wrapper.AddView(title);
TextView yearTitle = new TextView(this);
textParams.AddRule(LayoutRules.Below, title.Id);
i++;
yearTitle.Id = i;
yearTitle.Text = "Год: ";
yearTitle.LayoutParameters = textParams;
wrapper.AddView(yearTitle);
TextView year = new TextView(this);
year.Text = anime.Information.Year;
i++;
year.Id = i;
textParams.AddRule(LayoutRules.RightOf, yearTitle.Id);
year.LayoutParameters = textParams;
wrapper.AddView(year);
TextView genreTitle = new TextView(this);
textParams.AddRule(LayoutRules.Below, year.Id);
textParams.AddRule(LayoutRules.RightOf, img.Id);
genreTitle.LayoutParameters = textParams;
genreTitle.Text = "Жанр: ";
i++;
genreTitle.Id = i;
wrapper.AddView(genreTitle);
TextView genre = new TextView(this);
genre.Text = anime.Information.Genre;
textParams.AddRule(LayoutRules.RightOf, genreTitle.Id);
genre.LayoutParameters = textParams;
i++;
genre.Id = i;
wrapper.AddView(genre);
TextView gradeTitle = new TextView(this);
textParams.AddRule(LayoutRules.RightOf, img.Id);
textParams.AddRule(LayoutRules.Below, genre.Id);
gradeTitle.LayoutParameters = textParams;
gradeTitle.Text = "Оценка: ";
i++;
gradeTitle.Id = i;
wrapper.AddView(gradeTitle);
TextView grade = new TextView(this);
textParams.AddRule(LayoutRules.RightOf, gradeTitle.Id);
grade.LayoutParameters = textParams;
grade.Text = anime.Rating.Grade;
i++;
grade.Id = i;
wrapper.AddView(grade);
TextView votes = new TextView(this);
textParams.AddRule(LayoutRules.RightOf, grade.Id);
votes.Text = string.Format("( {0} )", anime.Rating.Votes);
i++;
votes.Id = i;
wrapper.AddView(votes);
LinearLayout linearList = FindViewById<LinearLayout>(Resource.Id.linearList);
linearList.AddView(wrapper);
i++;
}
private void\u buildUI(动物学家列表)
{
var i=1;
RelativeLayout.LayoutParams textParams=新的RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WrapContent,RelativeLayout.LayoutParams.WrapContent);
foreach(anList中的动画)
{
RelativeLayout包装器=新的RelativeLayout(此);
wrapper.LayoutParameters=新建ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent,ViewGroup.LayoutParams.WrapContent);
ImageView img=新的ImageView(本);
img.LayoutParameters=新视图组.LayoutParams(68,90);
img.Id=i;
SetImageURI(Android.Net.Uri.Parse(anime.Image.Url));
wrapper.AddView(img);
TextView title=新的TextView(此);
textParams.SetMargins(5,0,0,0);
textParams.AddRule(LayoutRules.RightOf,img.Id);
title.LayoutParameters=textParams;
i++;
title.Id=i;
title.Text=anime.title;
wrapper.AddView(标题);
TextView yearTitle=新的TextView(本);
textParams.AddRule(LayoutRules.down,title.Id);
i++;
yearTitle.Id=i;
yearTitle.Text=“ГöП:”;
yearTitle.LayoutParameters=文本参数;
wrapper.AddView(yearTitle);
TextView年=新的TextView(本);
year.Text=anime.Information.year;
i++;
年份Id=i;
textParams.AddRule(LayoutRules.RightOf,yearTitle.Id);
year.LayoutParameters=textParams;
wrapper.AddView(年);
TextView genreTitle=新的TextView(此);
textParams.AddRule(LayoutRules.Below,year.Id);
textParams.AddRule(LayoutRules.RightOf,img.Id);
genreTitle.LayoutParameters=textParams;
genreTitle.Text=“ЖааУ:”;
i++;
genreTitle.Id=i;
wrapper.AddView(genreTitle);
TextView流派=新的TextView(本);
genre.Text=anime.Information.genre;
textParams.AddRule(LayoutRules.RightOf,genreTitle.Id);
genre.LayoutParameters=textParams;
i++;
类型Id=i;
AddView(体裁);
TextView gradeTitle=新的TextView(此);
textParams.AddRule(LayoutRules.RightOf,img.Id);
textParams.AddRule(LayoutRules.Below,genre.Id);
gradeTitle.LayoutParameters=文本参数;
gradeTitle.Text=“ццццаa:”;
i++;
gradeTitle.Id=i;
wrapper.AddView(gradeTitle);
TextView等级=新的TextView(本);
textParams.AddRule(LayoutRules.RightOf,gradeTitle.Id);
grade.LayoutParameters=textParams;
grade.Text=anime.Rating.grade;
i++;
等级Id=i;
包装器。添加视图(等级);
TextView投票=新的TextView(本);
textParams.AddRule(LayoutRules.RightOf,grade.Id);
voates.Text=string.Format(“({0})”,anime.Rating.voates);
i++;
投票:Id=i;
wrapper.AddView(投票);
LinearLayout linearList=findviewbyd(Resource.Id.linearList);
linearList.AddView(包装器);
i++;
}
谁能帮我解决那个问题?我的错在哪里
解决
我试图重新设计那里的轮子…我需要做的是使用ListView和自定义行布局。它在Xamarin网站中有很好的文档记录-。你应该提交链接作为答案并接受它,这样问题就会被标记为已回答。