[英]Add Dynamic TableView in a Horizontal ScrollView
我试图找出一个人如何制作TableView而不在XML文件中指定它。 我有两个活动:
因此,这一行工作正常,但是当我尝试放置过多列时,显然几乎看不到数字。
第二项活动:
public class TableCreator extends Activity {
private String getRandomPoints(){
Random rn = new Random();
return new Integer(Math.abs(rn.nextInt()%3)).toString();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_creator);
Bundle extras = getIntent().getExtras();
int rows = extras.getInt("rows");
int columns = extras.getInt("columns");
TableLayout table = (TableLayout) findViewById(R.id.TableLayout);
for(int i=0;i<rows;i++){
TableRow row = new TableRow(this);
row.setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
for(int j=0;j<columns;j++){
TextView tv = new TextView(this);
tv.setText(getRandomPoints());
tv.setLayoutParams(new TableRow.LayoutParams(0,LayoutParams.WRAP_CONTENT,1));
tv.setGravity(Gravity.CENTER);
row.addView(tv);
}
table.addView(row);
}
}
}
和XML文件:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/TableLayout"
android:stretchColumns="0,1"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TableLayout>
之后,我尝试在xml文件中添加HorizontalScrollView,因此将TableView包含在其中。 这引发了一个异常,因为系统无法确定实际宽度和TableLayout将使用的高度...
我也尝试过以编程方式添加HorizontalScrollView,但是没有显示数字。 我可以说我在这个站点上仔细搜索过同样的问题,但是答案仍然与我无关。
在这里,您可以将Layout设置为水平滚动视图,tablelayout是子级
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".EvacRouteTableActivity" >
<TableLayout android:id="@+id/TableLayout01" android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="0">
<TableRow
android:id="@+id/TableRow01"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/TextView01"
android:background="@drawable/cell_shape"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" Evacuation Routes (To view route click name)"
android:width="340dp"
android:height="30dp"
android:textStyle="bold"></TextView>
</TableRow>
</TableLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.