android TableLayout动态添加单元格 - 没有显示任何内容

时间:2014-01-29 21:15:33

标签: android android-layout android-resources android-tablelayout android-inflate

我有动态添加单元格的TableLayout。不幸的是,不显示单元格(显示布局时,所有动态添加的部分似乎都不存在)。

我的布局定义的第一部分:

<TableLayout 
android:id="@+id/main_activity_details_calendar_main_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

现在有一个单元格xml定义:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_weight="1"
    >

    <TextView 
        android:id="@+id/main_activity_details_calendar_item_text_date"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1"
        android:gravity="left"
        />
    <TextView 
        android:id="@+id/main_activity_details_calendar_item_text_counter"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:maxLines="1"
        android:gravity="right"
        />

</LinearLayout>

现在我的代码中有:

public class MainActivity 
.....

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

......

calendar.onCreateView(view, inflater, container);

.....

return view;
}

protected class Calendar { //this is inner class

   protected TableLayout calendarMainGrid;
   public final static int NUMBER_OF_WEEKS = 2;
   public final static int NUMBER_OF_DAYS_IN_WEEK=7;

   public void onCreateView (View view, LayoutInflater inflater, ViewGroup container) {

      calendarMainGrid = (TableLayout) view.findViewById(R.id.main_activity_details_calendar_main_grid);

       for (int i=0; i<=NUMBER_OF_WEEKS; i++) { //we have <= as first row will be used for displaying the names of days

          TableRow newRow = new TableRow(calendarMainGrid.getContext());
          newRow.setLayoutParams (new TableRow.LayoutParams(android.widget.TableRow.LayoutParams.WRAP_CONTENT, 
                        android.widget.TableRow.LayoutParams.WRAP_CONTENT));

          for (int j=0; j<NUMBER_OF_DAYS_IN_WEEK; j++) {
              View cell = inflater.inflate(R.layout.main_activity_details_calendar_item, container, false);

               TextView t1 = (TextView) cell.findViewById(R.id.main_activity_details_calendar_item_text_date);
           TextView t2 = (TextView) cell.findViewById(R.id.main_activity_details_calendar_item_text_counter);
           t1.setText(i*NUMBER_OF_DAYS_IN_WEEK+j);
           t2.setText(i*NUMBER_OF_DAYS_IN_WEEK+j);
               newRow.addView(cell);


    }

       calendarMainGrid.addView(newRow, new TableLayout.LayoutParams(android.widget.TableLayout.LayoutParams.MATCH_PARENT, 
                        android.widget.TableLayout.LayoutParams.WRAP_CONTENT));
            }
        }

正如我所说,TableLayout中没有显示任何内容(正确显示相同布局的xml文件中的其他静态设置元素)。

此外,LogCat会显示以下警告:

01-29 22:03:31.599: W/ResourceType(8561): No package identifier when getting value for resource number 0x00000000

以及数字的下一个警告,直到14。

我将不胜感激。提前谢谢!

是的,我真的不明白为什么Android不能在一个单元格上提供任何日历,并且可以在一个单元格上提供自定义视图......现在我不是在5分钟内添加自定义日历,而是#&# 39;花费数小时从tablelayout创建日历视图(我也发现在我的情况下我不允许使用GridView,因为我的布局中已经有一个ScrollView)。 / p>

2 个答案:

答案 0 :(得分:0)

  

android:layout_width="match_parent"

尝试将其更改为: android:layout_width="wrap_content"

答案 1 :(得分:0)

两个错误:

应该是:

View cell = inflater.inflate(R.layout.main_activity_details_calendar_item, null);

和下一个:

t1.setText(Integer.toString(i*NUMBER_OF_DAYS_IN_ONE_WEEK+j));

否则没有自动转换,因为setText也带有int参数,这意味着在这种情况下是ressource id。

哦,我在那些“愚蠢”的错误上花了很多时间......

因此问题解决了!

相关问题