Adding Text views in multiple rows in Relative layout programmatically

[亡魂溺海] 提交于 2019-12-24 00:55:58

问题


I'm trying to achieve the following view in image programmatically. I am adding text view to relative layout dynamically. My problem is text view are added as required to right of each text view added prior. But after one row completion other text views are hidden due to the size of screen. How to add other remaining text view to next line after they added in one line. Whether my approach is wrong to achieve this. Please guide me. Thanks.

Here is code :

RelativeLayout llTags = (RelativeLayout)res.findViewById(R.id.layout_tags);
for (int i = 0; i < arrTags.size(); i++) {  
    View view =  (getActivity()).getLayoutInflater().inflate(R.layout.item_tags, null);
    tvTag = (TextView) view.findViewById(R.id.tv_tags);
    tvTag.setText(arrTags.get(i));
    view.setId(i+1);            
    RelativeLayout.LayoutParams lpFirst = new 
        RelativeLayout.LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    if(i!=0){
       lpFirst.addRule(RelativeLayout.RIGHT_OF,view.getId()-1);             
       lpFirst.setMargins(5,0,0,0);
    }
    llTags.addView(view, i, lpFirst);           
}             


回答1:


Finally after much googling got the solution from here :

https://github.com/ApmeM/android-flowlayout

Need to create custom flow layout instead of relative layout used.Custom flow layout will adjust child views accordingly in rows.



来源:https://stackoverflow.com/questions/17878741/adding-text-views-in-multiple-rows-in-relative-layout-programmatically

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