【问题标题】:android TableLayout with dynamically added cells - nothing displayed带有动态添加单元格的android TableLayout - 没有显示
【发布时间】:2014-01-29 21:15:33
【问题描述】:

我有动态添加单元格的 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 分钟内添加自定义日历,而是花费数小时从 tablelayout 创建日历视图(我还发现,在我的情况下,我不允许使用 GridView,因为我的布局中已经有一个 ScrollView)。

【问题讨论】:

  • 当我用完全在代码中创建的视图替换“j”循环“for (int j=0; j

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


【解决方案1】:

android:layout_width="match_parent"

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

【讨论】:

  • 请说明此解决方案如何解决手头的问题,以便将来访问此问题的人能够更好地理解。
  • 没有人,它不会改变任何东西。此外,LogCat 警告表明问题与资源有关。
  • 那是我的远景
  • 我认为可能是因为它是动态构建的,它仍然没有获得它的父级,因此 match_parent 可能导致宽度为 0.... .对不起:)
【解决方案2】:

两个错误:

应该是:

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

接下来:

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

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

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

这样问题就解决了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    相关资源
    最近更新 更多