Android 在水平滚动视图中添加动态表格视图
我试图找出如何在不在XML文件中指定的情况下创建TableView。我有两项活动:Android 在水平滚动视图中添加动态表格视图,android,tableview,horizontalscrollview,Android,Tableview,Horizontalscrollview,我试图找出如何在不在XML文件中指定的情况下创建TableView。我有两项活动: 获取行数和列数 生成具有给定行和列的表视图(单元格中填充0-2之间的随机数) 所以,这个很好,但是当我尝试放置太多的列时,很明显这些数字几乎看不到 第二项活动: public class TableCreator extends Activity { private String getRandomPoints(){ Random rn = new Random();
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);
}
}
}
公共类TableCreator扩展活动{
私有字符串getRandomPoints(){
Random rn=新的Random();
返回新整数(Math.abs(rn.nextInt()%3)).toString();
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.table_创建者);
Bundle extras=getIntent().getExtras();
int rows=extras.getInt(“rows”);
int columns=extras.getInt(“columns”);
TableLayout table=(TableLayout)findviewbyd(R.id.TableLayout);
对于(inti=0;i在这里,您可以将布局设置为水平滚动视图,而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>
谢谢您的回答,但我看不出您是如何解释我的问题的……我想了解HorizontalScrollView处理动态添加数据的方式……您建议我使用的形状很有帮助,但对于另一个问题……好的,更改了我的答案。很抱歉。再次感谢您的发布,但我再次表示TextView是以编程方式添加。“非动态”方式很简单,而且可以在短时间内完成…为了节省每个人的时间,我找到了解决方案。在某些方面,问题似乎不是因为添加了scrollView,但我添加LayoutParameters的方式是错误的…无论如何,请参见我在活动中所做的。
<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>