【发布时间】: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