简体   繁体   English

Android:在具有固定宽度的TableLayout上动态添加TextView

[英]Android: Dynamically add TextView on a TableLayout with fixed Width

I'm facing an issue on my android application. 我在android应用程序上遇到问题。 I want to dynamically create TableRow in a TableLayout. 我想在TableLayout中动态创建TableRow。 Each of this row needs to contain 4 TextViews. 该行的每一行都需要包含4个TextView。 I want the TextView like this: 我想要这样的TextView:

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

I manage to create the Tewtview dynamically but the size are not respected. 我设法动态创建Tewtview,但是大小不被尊重。 For example if my first textview contains a big text, it covers all the screen instead of only 40% and multiline. 例如,如果我的第一个textview包含大文本,它将覆盖整个屏幕,而不是仅覆盖40%和多行。

I read a lot of similar questions but can't find the solution here. 我读了很多类似的问题,但是在这里找不到解决方案。 Here is my layout file: 这是我的布局文件:

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

The Layout creates a static tab with a first row that is as I want it to be. 布局使用第一行创建一个静态选项卡,正如我所希望的那样。 But the rows added dynamically are not behaving the same way. 但是动态添加的行的行为方式不同。

My dynamic code: 我的动态代码:

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

My match object is only containing string with various size. 我的匹配对象仅包含各种大小的字符串。 Thanks a lot for your help 非常感谢你的帮助

The reason your dynamicly added TableRows don't do as you would like is that you do not set the LayoutParams for the TableRow nor the TextViews . 动态添加的TableRows不按照您的TableRows操作的原因是,您没有为TableRowTextViews设置LayoutParams

On each TextView you add LayoutParams like this: 在每个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 ) );

The last parameter of the LayoutParams -constructor is the layout_weight . LayoutParams -constructor的最后一个参数是layout_weight

You need to add the LayoutParams before addind the TextViews to the TableRow . 您需要在将TextViewsTableRow之前添加LayoutParams

You might need to add LayoutParams to your TableRow aswell. 您可能还需要将LayoutParams添加到TableRow

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM