将控件动态添加到tabhost时出现问题

时间:2011-01-18 07:12:33

标签: android android-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

1 个答案:

答案 0 :(得分:1)

斯坦格,我没有得到任何回复!好吧,我已经找到了解决方案。解决方案是我需要在将控件添加到TableRow时使用TableRow.LayoutParams而不是ViewGroup.LayoutParams。

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