【发布时间】:2014-10-17 02:03:48
【问题描述】:
我无法向右滚动,因为我的列是动态绘制的。我完全不知道。我尝试使用horizontalscrollview,但仍然无法向右滚动。任何解决方案将不胜感激。
xml
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tl"
android:weightSum="5"
>
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:text="test" />
<TableLayout android:stretchColumns="*"
android:layout_weight="3">
<TableRow>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="test" />
</TableRow>
</TableLayout>
</TableRow>
</TableLayout>
</HorizontalScrollView>
代码。
TableLayout tl, tl5;
tl = (TableLayout) findViewById(R.id.tl);
tl.removeAllViews();
TableRow tr3 = new TableRow(this);
TextView lblrecID11 = new TextView(this);
lblrecID11.setBackgroundResource(R.drawable.cell_shape);
lblrecID11.setText("column1");
lblrecID11.setGravity(Gravity.CENTER);
LayoutParams params44 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1.3f);
lblrecID11.setLayoutParams(params44);
tr3.addView(lblrecID11);
TextView lblrecName11= new TextView(this);
lblrecName11.setBackgroundResource(R.drawable.cell_shape);
lblrecName11.setText("column2");
lblrecName11.setGravity(Gravity.CENTER);
LayoutParams params3 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 1f);
lblrecName11.setLayoutParams(params3);
tr3.addView(lblrecName11);
TextView lblrecName12= new TextView(this);
lblrecName12.setBackgroundResource(R.drawable.cell_shape);
lblrecName12.setText("column3");
lblrecName12.setGravity(Gravity.CENTER);
lblrecName12.setLayoutParams(params3);
tr3.addView(lblrecName12);
tl5 = new TableLayout(this);
tl5.setStretchAllColumns(true);
LayoutParams params4 = new TableRow.LayoutParams(0, LayoutParams.MATCH_PARENT, 5f);
tl5.setLayoutParams(params4);
tr3.addView(tl5);
TableRow tr4 = new TableRow(this);
TextView lblrecID5 = new TextView(this);
lblrecID5.setText("Column Top 4");
lblrecID5.setBackgroundResource(R.drawable.cell_shape);
lblrecID5.setGravity(Gravity.CENTER);
TableRow.LayoutParams rowParams2 = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, TableRow.LayoutParams.WRAP_CONTENT);
rowParams2.span = 30;
lblrecID5.setLayoutParams(rowParams2);
tr4.addView(lblrecID5);// add the column to the table row here
TableRow tr5 = new TableRow(this);
for(int i=0;i<30;i++)
{
TextView lblrecID6 = new TextView(this);
lblrecID6.setText("test1010");
lblrecID6.setBackgroundResource(R.drawable.cell_shape);
lblrecID6.setGravity(Gravity.CENTER);
lblrecID6.measure(0, 0);
int width = lblrecID6.getMeasuredWidth();
myListWidth.add(width);
tr5.addView(lblrecID6);
}
tl5.addView(tr4);
tl5.addView(tr5);
tl.addView(tr3, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
for(int i = 0;i<5;i++)
{
TableRow tr = new TableRow(this);
TextView lblrecID = new TextView(this);
lblrecID.setBackgroundResource(R.drawable.cell_shape);
lblrecID.setText("testrow");
lblrecID.setSingleLine(true);
lblrecID.setGravity(Gravity.CENTER);
LayoutParams params22 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.3f);
lblrecID.setLayoutParams(params22);
tr.addView(lblrecID);
TextView lblrecName = new TextView(this);
lblrecName.setId(200+i);
lblrecName.setBackgroundResource(R.drawable.cell_shape);
lblrecName.setText("test123");
lblrecName.setGravity(Gravity.CENTER);
LayoutParams params = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
lblrecName.setLayoutParams(params);
tr.addView(lblrecName);
TextView lblrecName3 = new TextView(this);
lblrecName3.setBackgroundResource(R.drawable.cell_shape);
lblrecName3.setText(" ");
lblrecName3.setGravity(Gravity.CENTER);
lblrecName3.setLayoutParams(params);
tr.addView(lblrecName3);
TableLayout tl1 = new TableLayout(this);
LayoutParams params1 = new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 5f);
tl1.setStretchAllColumns(true);
tl1.setLayoutParams(params1);
TableRow tr2 = new TableRow(this);
for(int j=0;j<30;j++)
{
TextView lblrecID1 = new TextView(this);
lblrecID1.setBackgroundResource(R.drawable.cell_shape);
lblrecID1.setText("test1010");
lblrecID1.setGravity(Gravity.CENTER);
tr2.addView(lblrecID1);
}
tl1.addView(tr2);
tr.addView(tl1);
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
}
【问题讨论】:
-
我之前也遇到过类似的问题。尝试调整滚动视图的宽度和表格布局。
-
@hoomi 我试过了,但还是没有成功。
-
你可以试试
LinearLayout(水平方向)而不是TableLayout -
@kaushik 您的意思是在 holizontalscrollview 下方但在表格布局上方添加线性布局?
标签: android scrollview tablelayout