处理ScrollView中的LinearLayout

时间:2011-10-26 02:50:44

标签: 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 = (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&gt; ScrollView&gt; linearLayout&gt; tableLayout这样的层次结构。但是,现在上面的代码崩溃了应用程序。

为了填充表格我需要更改什么,以及为新创建的linearlayout添加视图?

1 个答案:

答案 0 :(得分:0)

我建议删除父LinearLayout。 Scrollview本身可以是父级,然后将一个LinearLayout作为其唯一的直接子级。

在您发布的代码中,您提到的是崩溃,您没有向“新创建的”LinearLayout添加任何视图,而是向TableLayout添加一个新行,如果我猜错了,就在里面你的滚动视图。这是完全正常的,不应该是崩溃的原因。