【问题标题】:LayoutInflater Mono for Android适用于 Android 的 LayoutInflater Mono
【发布时间】:2013-04-06 03:23:21
【问题描述】:

我想使用 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);
    }

【问题讨论】:

标签: c# xamarin.android layout-inflater


【解决方案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);
}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 2012-02-10
    相关资源
    最近更新 更多