1

LayouInflater を使用してプログラムで relativeLayout を作成したいのですが、AddView メソッドでエラーがスローされます。私のコードは次のとおりです。

<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);
    }
4

1 に答える 1

3

「1つしか表示されない」とはどういう意味ですか。複数のインスタンスを追加しようとしている場合は、毎回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);
}

これは、同じオブジェクトを複数回追加しようとしているためです。追加は無視されます。そして、ループ内で同じアイテムを繰り返し変更するため、「最後の」アイテムのみを追加したように見えます。

于 2012-02-28T08:28:37.047 に答える