2013-04-08 63 views
2

我已经在Android(树视图)中以编程方式构建了布局,现在我想向构建视图添加一个顶栏(topbar.xml)。在Android中以编程方式合并xml布局

所以我需要的是不是:

setContentView(scroll) 

喜欢的东西:

inflateInMyViewCalledScroll(topbar.xml) 
setContentView(scroll) 

感谢您的建议

回答

4

ScrollView只能有一个直接的孩子。

所以,你必须做这样的事情:

<ScrollView> 
    <LinearLayout android:id="@+id/foo" android:orientation="vertical"> 
     <!-- youll add topbar here, programmatically --> 
     <other things/> 
    </LinearLayout/> 
</ScrollView> 

然后在运行时,你会膨胀的顶栏

View topbar = getLayoutInflater().inflate(R.layout.topbar, null); 

和它foo

foo.addView(topbar, 0); 
添加为第一指标
+0

我很抱歉打扰,但你的诡计正在造成我一些问题。例如,我找不到我正在查找的资源(就像com.controller.R类没有指定的资源)。此外,我不希望顶栏被滚动,我添加滚动的资源,我想要滚动,我想在顶部的顶杆修复... – 2013-04-09 07:09:55

+0

好吧,我有一个问题,我能够获取布局,但我无法添加其他组件。我创建了一个新的LinearLayout并将其全部添加到它。谢谢!! – 2013-04-09 08:16:59

5

使用LayoutInflatertopbar.xml充气,把成果转化为scroll

getLayoutInflater().inflate(R.layout.topbar, scroll); 
+0

我已经试过这个,但我得到RuntimeException“无法启动活动...” – 2013-04-08 14:15:45

+0

@LuigiTib urzi:当您检查与此异常关联的LogCat中的Java堆栈跟踪时,您学到了什么? – CommonsWare 2013-04-08 14:17:24

+0

它说scrollView可以只托管一个直接的孩子......但我需要它有一个顶杆... – 2013-04-08 14:21:13

相关问题