如何在tablerow中设置多行textview

时间:2018-04-04 03:41:33

标签: android textview tablerow

我想在tablerow中创建多行textview并使用以下代码,但列的长文本覆盖整个宽度。如何在tablerow中设置 mutli-lines textview

enter image description here

enter image description here

    TableLayout getClassBookTable(ArrayList<String> titles, String json) {
        TableLayout tableLayout = new TableLayout(this);
        try {

            tableLayout.addView(getTitles(titles, false));
            JSONArray jsonArray = new JSONArray(json);
//            TableRow.LayoutParams params = new TableRow.LayoutParams(
//                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT, 1);
//            params.span = 1;
            for (int i = 0; i <jsonArray.length(); i++) {
                TableRow tableRow = new TableRow(this);
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                TextView snTextView = getTableTextView();
                snTextView.setText(jsonObject.getString("s.n."));
//                snTextView.setLayoutParams(params);
                tableRow.addView(snTextView);

                TextView bookTextView = getTableTextView();
                bookTextView.setText(jsonObject.getString("book"));
//                bookTextView.setLayoutParams(params);
                tableRow.addView(bookTextView);


                TextView subjectTextView = getTableTextView();
                subjectTextView.setSingleLine(false);
                subjectTextView.setText(jsonObject.getString("subjectString"));
//                subjectTextView.setLayoutParams(params);
                tableRow.addView(subjectTextView);


                TextView publisherTextView = getTableTextView();
                publisherTextView.setSingleLine(false);
                publisherTextView.setText(jsonObject.getString("publisher"));
//                publisherTextView.setLayoutParams(params);
                tableRow.addView(publisherTextView);


                TextView authorTextView = getTableTextView();
                authorTextView.setText(jsonObject.getString("author"));
//                authorTextView.setLayoutParams(params);
                tableRow.addView(authorTextView);

                tableLayout.addView(tableRow);


            }

EDITTED 当我添加以下代码    tableLayout.setShrinkAllColumns(真); tableLayout.setColumnStretchable(0,true); 它的布局如下所示 enter image description here

1 个答案:

答案 0 :(得分:0)

创建一个像这样的xml代码:

<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" 
android:stretchColumns="0"
android:shrinkColumns="*">    //shrink your columns like this  android:shrinkColumns="0"as per your requirement and make sure text view will wrap text.

<TableRow>
     <TextView
         android:id="@+id/question"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>

java代码:

TableLayout table_layout = (TableLayout) view.findViewById(R.id.table);
table_layout.setColumnShrinkable(1,true);

请尝试此代码,它可以帮助您。