2011-01-18 20 views
1

タブコントロール内でホストされているテーブルレイアウトにコントロールを動的に追加しようとしています。コードはエラーをスローせずに実行されますが、新しく追加されたコントロールは表示されません。あなたは、コードを見て、そして私が間違っていたステップを知らせる場合、私は感謝されます。私はポストへの応答を取得していないことをtabhostにコントロールを動的に追加する際の問題

<?xml version="1.0" encoding="utf-8"?> 
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <LinearLayout 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="5dp"> 
     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:padding="5dp" > 
      <TableLayout 
       android:id ="@+id/aTableLayout" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent">    
      </TableLayout> 
      <TableLayout 
       android:id="@+id/anotherTableLayout"   
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent">    
      </TableLayout>   
     </FrameLayout>   
</LinearLayout> 
</TabHost> 

package bajaj.dinesh.tablayout.test; 

import bajaj.dinesh.tablayout.test.R; 
import android.app.Activity; 
import android.app.TabActivity; 
import android.os.Bundle; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.TabHost; 
import android.widget.TableLayout; 
import android.widget.TableRow; 

public class TabLayoutTest extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 

     // Initialize a TabSpec for each tab and add it to the TabHost 
     spec = tabHost.newTabSpec("tag1").setIndicator("Tag 1") 
         .setContent(R.id.aTableLayout); 
     tabHost.addTab(spec); //Tag 1 

     spec = tabHost.newTabSpec("tag2").setIndicator("Tag 2") 
     .setContent(R.id.anotherTableLayout); 
     tabHost.addTab(spec); //Tag 2 

     FrameLayout frameLayout = tabHost.getTabContentView(); 
     TableLayout tableLayout = (TableLayout)frameLayout.findViewById(R.id.aTableLayout); 

     /* Create a new row to be added. */ 
     TableRow tableRow = new TableRow(this); 
     /* Create a Button to be the row-content. */ 
     Button button = new Button(this); 
     button.setText("Dynamic Button"); 
     button.setLayoutParams(new LayoutParams(
         LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT)); 
      /* Add Button to row. */ 
      tableRow.addView(button); 
     /* Add row to TableLayout. */ 
     tableLayout.addView(tableRow,new TableLayout.LayoutParams(
       LayoutParams.FILL_PARENT, 
       LayoutParams.WRAP_CONTENT)); 

    } // end of definition of onCreate method 
} // end of definition of TabLayoutTest class 
+0

こんにちは、質問が大変難しいですか?何か不足している場合は私に知らせて、私はそれを提供することができます。ありがとう。 –

答えて

1

スタンジェ:

コードが下に生産されます!さて、私は解決策を考え出しました。解決策は、私がのTableRowにコントロールを追加するときにTableRow.LayoutParamsなくViewGroup.LayoutParamsを使用する必要があるということです。

button.setLayoutParams(new TableRow.LayoutParams(
         LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT)); 
関連する問題