在我决定在这里问这个问题之前,我经常搜索了很多... 我想要实现的是动态创建布局,基本上是一些包含一些内容的行...... 我将模板设计为axml布局。这是:
<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++;
}
任何人都可以帮我解决这个问题吗?我的错误在哪里?
Sovled!
我试图在那里重新发明轮子......我需要做的是使用ListView和自定义行布局。 Xamarin网站上有详细记录 - here。