如何将TextViews放在一起

时间:2015-07-09 18:43:46

标签: android layout textview

我想创建一些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());
}

但是当TextViews到达屏幕的末尾时,它就像这样:

problem

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

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