【问题标题】:Android: Dynamically add TextView on a TableLayout with fixed WidthAndroid:在固定宽度的 TableLayout 上动态添加 TextView
【发布时间】:2011-12-13 22:11:46
【问题描述】:

我的 Android 应用程序遇到问题。我想在 TableLayout 中动态创建 TableRow。这一行中的每一行都需要包含 4 个 TextView。 我想要这样的 TextView:

  • textview1:屏幕的 40%
  • textview2:屏幕的 10%
  • textview3:屏幕的 10%
  • textview5:屏幕的 40%

我设法动态地创建了 Tewtview,但没有考虑到大小。例如,如果我的第一个 textview 包含一个大文本,它会覆盖整个屏幕,而不是仅 40% 和多行。

我阅读了很多类似的问题,但在这里找不到解决方案。这是我的布局文件:

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
android:layout_marginBottom="30dip"
android:id="@+id/journee_contenu">

<TableRow>
  <!-- Column 1 -->
  <TextView
     android:id="@+id/tbl_txt1"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:layout_weight="4"
     android:text="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
      />

  <!-- Column 2 -->
  <TextView
     android:id="@+id/tbl_txt2"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="bbbbb"
     />

  <!-- Column 3 -->
  <TextView
     android:id="@+id/tbl_txt3"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:text="ccccc"
      />
      <!-- Column 4 -->
  <TextView
     android:id="@+id/tbl_txt4"
     android:layout_width="0dip"
     android:layout_height="wrap_content"
     android:layout_weight="4"
     android:text="ddddddddddddddddddddddddddddddddddddddddddddddd"
      />
</TableRow>

布局创建了一个静态选项卡,其中包含我想要的第一行。但是动态添加的行的行为方式不同。

我的动态代码:

    TableLayout genLayout = (TableLayout) findViewById(R.id.journee_contenu);
for (MatchWrapper match : matchs) {
    TableRow tr1 = new TableRow(this);
    TextView tvEq1 = new TextView(this);
    tvEq1.setText(match.getEquipe1());
    TextView tvSc1 = new TextView(this);
    tvSc1.setText(match.getScore_equipe1());
    TextView tvSc2 = new TextView(this);
    tvSc2.setText(match.getScore_equipe2());
    TextView tvEq2 = new TextView(this);
    tvEq2.setText(match.getEquipe2());
    tr1.addView(tvEq1);
    tr1.addView(tvSc1);
    tr1.addView(tvSc2);
    tr1.addView(tvEq2);
    genLayout.addView(tr1);
}

我的匹配对象只包含各种大小的字符串。非常感谢您的帮助

【问题讨论】:

    标签: android textview width tablelayout fixed


    【解决方案1】:

    您动态添加的TableRows 没有按照您的意愿进行操作的原因是您没有为TableRowTextViews 设置LayoutParams

    在每个 TextView 上添加 LayoutParams,如下所示:

    tvEq1.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 4 ) );
    tvSc1.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1 ) );
    tvSc2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1 ) );
    tvEq2.setLayoutParams( new TableRow.LayoutParams( 0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 4 ) );
    

    LayoutParams-构造函数的最后一个参数是layout_weight

    您需要先添加LayoutParams,然后再将TextViews 添加到TableRow

    您可能还需要将LayoutParams 添加到您的TableRow

    【讨论】:

    • 非常感谢卡斯帕!我尝试了很多,但看起来就是这么简单!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-22
    • 2011-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多