java 动态添加到水平滚动视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10247414/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamically add to horizontalscrollview
提问by phalt
I've followed a few tutorials online that show you how to create a static horizontalscrollview with multiple xml files.
我已经在网上学习了一些教程,向您展示了如何使用多个 xml 文件创建静态水平滚动视图。
However I would like to be able to grab content from a database, fill a new view (from a stock xml layout) with the content and then add it to the horizontalscrollview.
但是,我希望能够从数据库中获取内容,用内容填充新视图(来自库存 xml 布局),然后将其添加到水平滚动视图。
Are there any tutorials that show you how to add dynamic views to a horizontalscrollview?
是否有任何教程向您展示如何向水平滚动视图添加动态视图?
回答by Arif Nadeem
That is easy,
那很容易,
Your HorizontalScrollView must contain a container like a LinearLayout or a RelativeLayout, grab an instance to that Layout in your activity, and add the views as required...
您的 HorizontalScrollView 必须包含一个类似 LinearLayout 或 RelativeLayout 的容器,在您的活动中获取该布局的实例,并根据需要添加视图...
LinearLayout yourLayout = (LinearLayout)findViewById(R.id.someID);
and then iterate through the number of items in your database and keep adding the views to your Layout until end like this...
然后遍历数据库中的项目数并继续将视图添加到布局中,直到像这样结束...
for (int i = 0; i < yourData.size(); i++) {
TextView tv = new TextView(getApplicationContext());
tv.setText(yourData.get(i));
yourLayout.addView(tv);
}
回答by MAC
*R.layout.colum*n is a another layout which you want to add.
* R.layout.colum*n 是您要添加的另一种布局。
<HorizontalScrollView ...>
<LinearLayout android:id="@+id/row" ..>
</LinearLayout>
</HorizontalScrollView>
LinearLayout featureLayout = (LinearLayout) View.inflate(YourActivity.this,R.layout.column, null);
row.addView(featureLayout);