开发者

Setting the layout_weight of the TextView under the TableRow

开发者 https://www.devze.com 2023-03-20 01:00 出处:网络
This question is actually related to this post Set the layout weight of a TextView programmatically Based on the answers I just need to set the TextView layout params as follow and set the stretchCol

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

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

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

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"&g开发者_如何学Got;
    <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>

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

Thanks,

artsylar


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

android: two issues using Tablerow+TextView in Tablelayout

How to make a TableLayout from XML using programmable way ?

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

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>

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号