Is there an Android Layout that can be set to automatically wrap “exceding” elements to the next line?

限于喜欢 提交于 2019-12-24 11:27:18

问题


In an Android app, I am trying to dynamically populate a TableLayout from an array, and I want each TableRow to hold at maximum 3 elements.

The way I had to implement it (code below), feels a lot like dynamically creating HTML tables and I was wondering if Android had more ad-hoc layout facilities that don't make me feel as if I am forcing my design idea over a more generic Layout like TableLayout.

Ideally, I would just like to deal with a layout that:

  • is aware of the number of elements I want per row (maybe via configuration?),
  • automatically stacks the TextViews horizontally, wrapping them on the next row only if they fill up the previous, so that I could simply cycle through the array elements and do myLayout.add(TextView).

Do we have something like that, or there's no other better way than handcrafting it?

In the latter case, how would you've done that?

TableLayout tab_lay = (TableLayout) viewRef.findViewById(R.id.myTableLayout);

TableRow table_row = new TableRow(contextRef);

int col_counter = 0;
for (TextView aTextView : arrayOfTextViews) {

    table_row.addView(aTextView);
    col_counter++;

    if (col_counter == 3) {
        tab_lay.addView(table_row);
        table_row = new TableRow(contextRef);
        col_counter = 0;
    }
}

来源:https://stackoverflow.com/questions/28380163/is-there-an-android-layout-that-can-be-set-to-automatically-wrap-exceding-elem

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