2011-07-14 10 views
2

下のTextViewのlayout_weightの設定この質問は実際にこのポストに私はちょうどstretchColumnプロパティに従うと、設定されますが、追加することによるようTextViewのレイアウトのparamsを設定する必要が回答に基づいてSet the layout weight of a TextView programmaticallyのTableRow

を関連していますコードをマイニングすると、textViewがテーブルレイアウトから消えます。

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

だから、ここで私が動的に35%、5%、60%の重量で3列と行を追加したい私のコードです。私のコードに何が間違っているか教えてください。

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ファイルで、

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

私も0にlayout_widthを設定しようとしたが、それでもそれは動作しませんでした。

おかげで、

答えて

6

artsylar
はあなたにみんなありがとう! 最後に、次の記事を参照して問題を解決することができました。

android: two issues using Tablerow+TextView in Tablelayout

How to make a TableLayout from XML using programmable way ?

私の作業コードについては、以下を参照してください。

これは、行の動的追加が行われる場所です。

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

<?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はすべてここに設定しました。

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

非常に良い。正しく動作します! –

+0

ありがとう@AliMohammadi – artsylar

関連する問題