【发布时间】:2026-01-03 22:55:02
【问题描述】:
我是 android dev 的初学者,我在以下方面遇到困难。 我想创建一个带有以编程方式创建的表格布局的片段。 我首先从一个 Activity 而不是我这样做的片段开始:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View view = getLayoutInflater().inflate(R.layout.activity_testtable, null,false);
fillLayout(view);
setContentView(view);
}
protected void fillLayout(View rootView) {
TableLayout tl;
TableRow tr;
TextView tv;
int i = 0;
tl = (TableLayout) rootView.findViewById(R.id.time_range);
for (i = 0 ; i < 10 ; i++ ) {
tv = new CellWorkshopTable(this);
tv.setText("11h00 - 14h40" );
tr = new TableRow (this);
tr.addView(tv);
tl.addView(tr);
}
[...]
}
效果很好,表格是根据我的需要在 2 轴滚动等创建的。然后我想将其设置为片段,所以我在片段中执行了以下操作:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View aView = inflater.inflate(R.layout.activity_testtable, container, false);
fillLayout(aView);
return aView;
}
所以当我提交片段时什么都没有出现,logcat 中没有错误,没有崩溃,似乎一切都在那里,但什么都没有显示。
这里是布局,目的是要有完整的结构,加上tableRow和cells(都是textviews):
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/time_table"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
android:padding="0dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="0dp" >
<TableLayout
android:id="@+id/time_range"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
<HorizontalScrollView
android:id="@+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="0dp" >
<TableLayout
android:id="@+id/header_rooms"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</TableLayout>
<TableLayout
android:id="@+id/time_table_content"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
</ScrollView>
所以在写这个问题的时候我尝试通过移除HorizontalScrollView和他的childs来简化布局,然后显示第一个tablelayout中添加的TableRows
移除 HorizontalScrollView 但保留 TableLayout+id/header_rooms 和 TableLayout+id/time_table_content,使 header_rooms 正常工作, time_table_content KO。
那么片段是否有布局数量限制,我该如何规避呢?
【问题讨论】: