2012-02-27 42 views
1

我想用LayouInflater以编程方式创建我的RelativeLayout但将在AddView方法的错误,继承人是我的代码:LayoutInflater单为Android

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/relativeAgedSummary"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</RelativeLayout> 

ViewGroup relativeAgedSummary=FindViewById<ViewGroup>(Resource.Id.relativeAgedSummary); 
View view = this.LayoutInflater.Inflate(
           Resource.Layout.aged_summary_list_item, 
           relativeAgedSummary, false);  

for (int i = 0; i < agedValues.Count; i++) { 
    var row = agedValues.ElementAt(i); 
    if(row.Range == 0 && row.IsTotal == false) 
    { 
     RelativeLayout rl = view.FindViewById<RelativeLayout>(
              Resource.Id.relativeLayoutAgedSummaryItem); 
     rl.SetBackgroundResource(Resource.Color.lightBlue); 
     if(contA==0) 
     { 
      TextView tv = (TextView)view.FindViewById(Resource.Id.txtAgedSummary); 
      tv.SetText(labelsAgedSummary[row.Range], TextView.BufferType.Normal); 
      contA++; 
     }   
     relativeAgedSummary.AddView(view); 
    } 
+0

它抛出的错误是什么? http://docs.xamarin.com/android/advanced_topics/android_debug_log – 2012-02-27 19:47:58

+0

一个固定的,但现在我想添加更多的实际布局,它只出现一个 – arkmetal 2012-02-27 20:00:09

回答

3

你是什么意思“它只会出现一个”。如果你想多个实例添加到relativeAgedSummary,那么你将不得不每次都重新膨胀aged_summary_list_item

for (int i = 0; i < agedValues.Count; i++) 
{ 
    View view = this.LayoutInflater.Inflate(
        Resource.Layout.aged_summary_list_item, 
        relativeAgedSummary, false); 

    var row = agedValues.ElementAt(i); 

    // the logic here... 

    relativeAgedSummary.AddView(view); 
} 

这是因为你正试图添加同一对象不止一次。它会忽略添加。然后在循环中重复修改相同的项目,从而导致它看起来只添加了“最后一个”项目。