如何将TextViews放在一起

时间:2021-10-26 11:30:40

I want to create some TextViews and put them next to each other and if TextViews reached to end of page, it starts from below of previous TextViews. I wrote code for putting TextViews next to each other as below:

我想创建一些TextView并将它们放在一起,如果TextViews到达页面末尾,它将从之前的TextViews的下方开始。我编写了将TextViews放在一起的代码,如下所示:

RelativeLayout main = (RelativeLayout) inflater.inflate(R.layout.activity_main, null);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
for(int i=0; i<10; i++){
    TextView text = new TextView(this);
    text.setText("Text"+String.valueOf(i));
    text.setId(i);
    main.addView(text, params);
    params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.RIGHT_OF, text.getId());
}

but when TextViews reaches end of screen it be similar this:

但是当TextViews到达屏幕的末尾时,它类似于:

如何将TextViews放在一起

how can I solve this problem?

我怎么解决这个问题?

1 个解决方案

#1


0  

I think the most elegant way to do it is to use a adapter view like RecyclerView. You can define max number of items in each row. It works like ListView but with more features. Read about it here: https://developer.android.com/training/material/lists-cards.html http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html

我认为最优雅的方法是使用像RecyclerView这样的适配器视图。您可以定义每行中的最大项目数。它的工作方式与ListView类似,但功能更多。在这里阅读:https://developer.android.com/training/material/lists-cards.html http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html

#1


0  

I think the most elegant way to do it is to use a adapter view like RecyclerView. You can define max number of items in each row. It works like ListView but with more features. Read about it here: https://developer.android.com/training/material/lists-cards.html http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html

我认为最优雅的方法是使用像RecyclerView这样的适配器视图。您可以定义每行中的最大项目数。它的工作方式与ListView类似,但功能更多。在这里阅读:https://developer.android.com/training/material/lists-cards.html http://blog.sqisland.com/2014/12/recyclerview-grid-with-header.html