Android 在滚动视图中处理线性布局
最初,我有LinearLayout>ScrollView>TableLayout和下面的代码:Android 在滚动视图中处理线性布局,android,Android,最初,我有LinearLayout>ScrollView>TableLayout和下面的代码: LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.photos_layout, null); setContentView(layout); // TODO Auto-generated method stub //get the tableLayout which we created in main.xml tl =
LinearLayout layout = (LinearLayout) View.inflate(this, R.layout.photos_layout, null);
setContentView(layout);
// TODO Auto-generated method stub
//get the tableLayout which we created in main.xml
tl = (TableLayout)findViewById(R.id.tableLayout1);
for(int i = 0; i < names.length; i++) {
//Create a new row to be added.
TableRow tr = new TableRow(this);
//Create text views to be added to the row.
TextView tv1 = new TextView(this);
TextView tv2 = new TextView(this);
//Put the data into the text view by passing it to a user defined function createView()
createView(tr, tv1, Integer.toString(i+1));
createView(tr, tv2, names[i]);
//Add the new row to our tableLayout tl
tl.addView(tr);
}
问题是scrollView只能有一个子视图,所以如果我想在tableLayout之外添加更多视图,我就不能。为了克服这个问题,我创建了像LinearLayout>ScrollView>LinearLayout>tableLayout这样的层次结构。但是,现在上面的代码使应用程序崩溃
为了填充表格以及向新创建的linearlayout添加视图,我需要更改什么?我建议删除父linearlayout。Scrollview本身可以是父级,然后有一个LinearLayout作为其唯一的直接子级
在您发布的代码中,您提到的代码崩溃了,您没有向新创建的LinearLayout添加任何视图,而是向TableLayout添加新行,如果我猜正确的话,它位于scrollview中。这是完全正常的,不应该是崩溃的原因。如果发生了崩溃,应该在崩溃日志后的此处包含LogCat输出。还有一些XML?首先,发布崩溃的stacktrace。其次,为什么不完全用xml或完全以编程方式创建整个布局?如果要以编程方式执行此操作,则应定义布局参数,以便将视图添加到其父视图中。此外,scrollview只能有一个直接子级。因此,您可以在scrollview中的linearlayout中放置所需的任何内容。另外,我能问一下你到底想做什么吗?你可能把你的浏览方式弄得太复杂了……现在这只是一个了解android的测试应用。我有我的活动,我想把表格布局和一些按钮放进去。它会比屏幕大,所以我把它放在一个滚动视图中。但我想我会接受你的建议,并以程序化的方式完成这一切;我讨厌搞清楚如何连接布局和代码。@Adam发布日志。没有这个我们帮不了你。另外,您可能只需要一个GridView或类似的东西。