2011-01-18 2 views
1

탭 컨트롤 내부에서 호스팅되는 테이블 레이아웃에 컨트롤을 동적으로 추가하려고합니다. 오류가 발생하지 않고 코드가 실행되지만 새로 추가 된 컨트롤은 나타나지 않습니다. 코드를 살펴보고 어떤 단계가 잘못된지 알려 주시면 감사하겠습니다.탭 호스트에 컨트롤을 동적으로 추가 할 때 문제가 발생했습니다.

아래 코드는 생성됩니다

<?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에 추가하는 동안 ViewRowoutParams가 아닌 TableRow.LayoutParams를 사용해야한다는 것입니다.

button.setLayoutParams(new TableRow.LayoutParams(
         LayoutParams.FILL_PARENT, 
         LayoutParams.WRAP_CONTENT));