【问题标题】:how to set multilines textview in tablerow如何在tablerow中设置多行textview
【发布时间】:2018-04-04 03:41:33
【问题描述】:

我想在 tablerow 中创建多行 textview 并使用以下代码,但 column 的长文本覆盖了整个宽度。如何在表格中设置多行文本视图

    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);


            }

已编辑 当我添加以下代码时 tableLayout.setShrinkAllColumns(true); tableLayout.setColumnStretchable(0, true); 其布局如下图所示

【问题讨论】:

  • 您的预期输出是什么以及您从上面的代码中得到什么输出?您需要提供有关您的问题的更多信息
  • 分享你的xml代码
  • 抱歉没有xml文件只有代码
  • @nilesh 我已经添加了它的截图

标签: android textview tablerow


【解决方案1】:

像这样创建一个 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);

请试试这个代码,它对你有帮助。

【讨论】:

  • 我添加了以下代码 tableLayout.setShrinkAllColumns(true); tableLayout.setColumnStretchable(0, true);还是有问题,上面加了截图
猜你喜欢
  • 2011-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 2011-09-16
  • 2015-02-28
  • 1970-01-01
相关资源
最近更新 更多