开发者

Problem in adding controls to a tabhost dynamically

I am trying to add a control dynamically to a table layout that is hosted inside a tab control. Though the code runs without throwing any error, the newly added control doesn't appear. I would be grateful if you have a look at the code, and let me know which step is incorrect.

The code is produced below:

<?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>          
        开发者_StackOverflow中文版</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


Stange that I didn't get any reply to the post! Well, I have figured out the solution. The solution is that I need to use TableRow.LayoutParams and not ViewGroup.LayoutParams while adding the control to a TableRow.

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜