设置的TableRow下的TextView的layout_weightTableRow、TextView、layout_weight

2023-09-05 10:02:35 作者:穿着凉鞋闯江湖

这个问题实际上是与此相关的帖子Set一个TextView的布局重编程

This question is actually related to this post Set the layout weight of a TextView programmatically

根据我只需要设置TextView的布局PARAMS如下,并设置stretchColumn物业的答案,但通过添加以下code矿,它使从表布局的TextView中消失。

Based on the answers I just need to set the TextView layout params as follow and set the stretchColumn Property, but by adding the following code to mine, it makes the textView disappear from the Table Layout.

TextView tv = new TextView(v.getContext());
tv.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));

所以,这里就是我要动态地3列有35%,5%和60%的重量增加一排我的code。请让我知道什么是错我的code。

So, here is my code where I want to dynamically add a row with 3 columns with 35%, 5% and 60% weight. Please let me know what is wrong with my code.

private void addTableRow(int resIdTitle, String strValue){

        TableRow tr = new TableRow(this);

        // Add First Column
        TextView tvTitle = new TextView(this);
        tvTitle.setText(resIdTitle);
        tvTitle.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT, 0.35f));
        tr.addView(tvTitle);

        // Add 2nd Column
        TextView tvColon = new TextView(this);
        tvColon.setText(" : ");
        tvColon.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT, 0.05f));
        tr.addView(tvColon);

        // Add the 3rd Column
        TextView tvValue = new TextView(this);
        tvValue.setText(strValue);
        tvValue.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
                LayoutParams.WRAP_CONTENT, 0.60f));
        tr.addView(tvValue);

        // Finally add it to the table
        tl.addView(tr, new TableLayout.LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
        Log.d(TAG, "Row Added");
    }

和这里是我的xml文件,

And here is my xml file,

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1">
    <TableLayout
      android:id="@+id/tl_cam_get_param"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:title="@string/strGetParamTitle" 
      android:scrollbars="vertical"       
      android:shrinkColumns="0">
        </TableLayout>
</ScrollView>

我也曾尝试layout_width设置为0,但仍然没有奏效。

I have also tried setting the layout_width to 0, but still it didn't work.

谢谢, artsylar

Thanks, artsylar

推荐答案

谢谢大家! 最后,我能够通过参考以下职位,以解决我的问题。

Thank you everyone! Finally, I was able to solve my problem by referencing to the following posts.

android:在Tablelayout 使用的TableRow + TextView的两个问题

android: two issues using Tablerow+TextView in Tablelayout

How采用可编程的方式,利用XML一个TableLayout?

请参考下我的工作code。

Refer below for my working code.

这就是动态的行添加完成。

This is where the dynamic adding of rows is done.

TableLayout tl = (TableLayout)findViewById(R.id.table_layout);

private void addTableRow(int resIdTitle, String strValue){

    LayoutInflater inflater = getLayoutInflater();
    TableRow tr = (TableRow)inflater.inflate(R.layout.table_row, tl, false);

    // Add First Column
    TextView tvTitle = (TextView)tr.findViewById(R.id.tvTitle);
    tvTitle.setText(resIdTitle);

    // Add the 3rd Column
    TextView tvValue = (TextView)tr.findViewById(R.id.tvValue);
    tvValue.setText(strValue);

    tl.addView(tr);
}

这是table_layout.xml

This is the table_layout.xml

 <?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1">
    <TableLayout
      android:id="@+id/table_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:title="@string/strGetParamTitle" 
      android:scrollbars="vertical"       
      android:shrinkColumns="0">
    </TableLayout>
</ScrollView>

和最后的table_row.xml。我在这里设置的所有layout_params。

And finally the table_row.xml. I've set all the layout_params here.

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android">
   <TextView  
          android:id="@+id/tvTitle"
          android:paddingRight="3dip"
          android:layout_width="0px"
          android:layout_weight="0.35"/>
   <TextView  android:text=":"
          android:layout_width="0px"
          android:layout_weight="0.05"/>
   <TextView
            android:id="@+id/tvValue"
            android:paddingLeft="3dip"
            android:layout_width="0px"
            android:layout_weight="0.6"
          /> 
</TableRow>