适用于Android的LayoutInflater Mono [英] LayoutInflater Mono for Android

查看:106
本文介绍了适用于Android的LayoutInflater Mono的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用LayouInflater以编程方式创建我的relativeLayout,但是在AddView方法上抛出错误,这是我的代码:

I want to create programatically my relativeLayout using LayouInflater but it throws an error on AddView method, heres is my code:

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

推荐答案

您的意思是它只出现一个".如果您尝试将多个实例添加到relativeAgedSummary,则每次都必须重新填充aged_summary_list_item:

What do you mean "it only appears one". If you are trying to add multiple instances to relativeAgedSummary then you will have to re-inflate the aged_summary_list_item each time:

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

这是因为您试图多次添加同一对象.它将忽略添加.然后,在循环中,您反复修改同一项目,从而使其看起来像仅添加了最后一个"项目.

This is because you are trying to add the same object more than once. It will ignore the add. And then in the loop you modify the same item repeatedly, thus resulting in it looking like it added only the "last" item.

这篇关于适用于Android的LayoutInflater Mono的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆