【问题标题】:TextView displaying text in One line when adding rows programatically in Table Layout在表格布局中以编程方式添加行时,TextView 在一行中显示文本
【发布时间】:2014-01-27 23:25:51
【问题描述】:

在我的 xml 文件中的一些视图之后,我有一个表格布局。下面是代码。

<ScrollView>
   <LinearLayout>

..................................
.....................................
<Button
         android:id="@+id/fetchbutton"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="show details"
         android:layout_margin="6dip"
         android:layout_gravity="center_horizontal"
         android:onClick="buttonOnClick"/>

         <TableLayout
            android:id="@+id/mytable_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:orientation="vertical"
             >
        </TableLayout>

    </LinearLayout>
</ScrollView>

以下是以编程方式为表格布局添加行的代码:

public void buttonOnClick(View view)
    {
        showData();
    }
    public void showData()
    {

        List<DataSource> data = dbHelper.getData(selectedoption);
        TableLayout tl = (TableLayout)findViewById(R.id.mytable_layout);

        for(DataClass dataclass:data)
        {
            TableRow tr = new TableRow(ViewScreen.this);

            TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);

            tr.setLayoutParams(params);   

            TextView labelTV = new TextView(ViewScreen.this);
            labelTV.setText(dataclass.getColumnName());
            labelTV.setTextColor(Color.RED);
            TableRow.LayoutParams paramsTV1 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
            labelTV.setLayoutParams(paramsTV1);
            tr.addView(labelTV);

            // Creating a TextView to house the value of the after-tax income
            TextView valueTV = new TextView(ViewScreen.this);
            valueTV.setText(dataclass.getColumnValue());
            valueTV.setTextColor(Color.BLACK);
            TableRow.LayoutParams paramsTV2 = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
            valueTV.setLayoutParams(paramsTV2);
            tr.addView(valueTV);

            // Adding the TableRow to the TableLayout
            tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


        }

    }

所以按照上面的代码,它工作正常。但是表格行总是一行。根据内容,table row height 不是increased。取而代之的是文本的一部分离开屏幕并且不可见。有人可以帮忙解决这个问题吗?

图片:

【问题讨论】:

  • android:orientation="vertical" 没有这个
  • 一开始我没有包含这个 android:orientation="vertical"。所以,我只是尝试添加这个。它在这两种情况下都不起作用。
  • 能否显示截图。
  • 为了更好的解释也添加了截图。
  • 您必须为黑色的 TextView 提供固定的宽度和高度来 wrap_content。希望这可能会有所帮助。

标签: android tablelayout tablerow


【解决方案1】:

创建一个自定义的 single_row.xml 如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayoutSingleRowRecordsDetailedView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff" >

    <TextView
        android:id="@+id/textViewSingleRowRecordsDetailedViewCategoryField"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="10dp"
        android:text="TextView"
        android:textColor="#04B4AE"
        android:textSize="15dp"
        android:typeface="sans" />

    <TextView
        android:id="@+id/textViewSingleRowRecordsDetailedViewCategoryFieldValue"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginRight="10dp"
        android:text="TextView"
        android:textColor="#000000"
        android:textSize="15dp"
        android:typeface="sans" />

</RelativeLayout>

然后在我的java代码中:

...
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                final View view = inflate.inflate(R.layout.single_row_records_detailed_view, null);

                TextView tv_field_name = (TextView) view.findViewById(R.id.textViewSingleRowRecordsDetailedViewCategoryField);
                TextView tv_field_value = (TextView) view.findViewById(R.id.textViewSingleRowRecordsDetailedViewCategoryFieldValue);

                if(al_field_value.get(i).equals(""))
                {
                    null_count = null_count + 1;
                    continue;
                }
                else
                {
                    tv_field_name.setText(get_field_name(al_cat_field_id.get(i)));
                    tv_field_value.setText(al_field_value.get(i));
                }

                linearLayout_details.addView(view);
...

【讨论】:

  • 请仔细阅读我的问题。如果我使用这个线性布局参数,视图将根本不可见。即使我将这些参数应用于我的文本视图,例如 TableRow.LayoutParams paramsTV1 = new TableRow.LayoutParams(150, TableRow.LayoutParams.WRAP_CONTENT); labelTV.setLayoutParams(paramsTV1);它也不起作用。我的问题是行的高度不会随着内容的增加而增加。
  • 不幸的是,我无法理解为什么我之前的回答无法解决您的问题。很抱歉,但您绝对可以自定义布局。检查我编辑的答案。请稍等。我想展示我做了什么来达到你想要的效果。
  • 我不能硬编码 :( 有没有其他方法让它不应该使用 android:layout_width="120dp" ?
  • 我不认为你必须做更多的代码来应用我的修复。但我可以向你保证,你会达到你想要的输出。这是肯定的。不要那么静态,要动态。你至少可以试一试。首先尝试你的助手向你展示的方式,当你完成了你的目标,试着找出你以前的代码没有发生的原因。这就是你学习的方式。标记我的话。
【解决方案2】:

尝试让你的 Textview 多行。

在您的代码中添加以下行以使 textview 多行。

yourtextview.setSingleLine(false);

希望对你有帮助

【讨论】:

  • @Ark : 你有没有尝试将两个参数都设置为 wrap_contents,试试多行
  • 也检查过。没用。我的代码对你有用吗?我确定它没有:(不知道怎么回事,请帮忙。
【解决方案3】:

为您的表格布局设置以下内容:-

tablename.setColumnShrinkable(ColumnIndex, true); 

【讨论】:

    【解决方案4】:

    你可以设置tablelayout的shrinkable属性来得到你想要的效果。来自 Android 开发者网站的文档:

    列的宽度由单元格最宽的行定义 那一栏。但是,TableLayout 可以将某些列指定为 通过调用 setColumnShrinkable() 或 setColumnStretchable()。如果标记为可收缩,列宽可以 缩小以使表格适合其父对象。如果标记为 可拉伸,它可以扩大宽度以适应任何额外的空间。总数 表格的宽度由其父容器定义。这很重要 请记住,一列既可以收缩也可以拉伸。在 在这种情况下,该列将更改其大小以始终用完 可用空间,但永远不会更多。最后,您可以隐藏一列 调用 setColumnCollapsed()。

    例子:

    <TableLayout
        android:id="@+id/fragment_print_job_detail_info_group"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:layout_margin="@dimen/image_button_margin"
        android:background="@drawable/round_corner_white_background_shape" >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-15
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多