Dynamically add to horizontalscrollview

ぃ、小莉子 提交于 2019-12-22 08:57:53

问题


I've followed a few tutorials online that show you how to create a static horizontalscrollview with multiple xml files.

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.

Are there any tutorials that show you how to add dynamic views to a horizontalscrollview?


回答1:


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...

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);
}



回答2:


*R.layout.colum*n is a another layout which you want to add.

<HorizontalScrollView ...>
   <LinearLayout android:id="@+id/row" ..>

   </LinearLayout>
</HorizontalScrollView>

LinearLayout featureLayout = (LinearLayout) View.inflate(YourActivity.this,R.layout.column, null);
row.addView(featureLayout);


来源:https://stackoverflow.com/questions/10247414/dynamically-add-to-horizontalscrollview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!