Android 向tabhost动态添加控件时出现问题
我试图将控件动态添加到托管在选项卡控件内的表布局中。虽然代码运行时没有抛出任何错误,但新添加的控件不会出现。如果您查看一下代码,并告诉我哪一步不正确,我将不胜感激 代码生成如下:Android 向tabhost动态添加控件时出现问题,android,android-tabhost,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" andro
<?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
包bajaj.dinesh.tablayout.test;
导入bajaj.dinesh.tablayout.test.R;
导入android.app.Activity;
导入android.app.TabActivity;
导入android.os.Bundle;
导入android.view.ViewGroup.LayoutParams;
导入android.widget.Button;
导入android.widget.FrameLayout;
导入android.widget.TabHost;
导入android.widget.TableLayout;
导入android.widget.TableRow;
公共类TabLayoutTest扩展了TabActivity{
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost TabHost=getTabHost();
TabHost.TabSpec;
//为每个选项卡初始化TabSpec并将其添加到TabHost
spec=tabHost.newTabSpec(“tag1”).setIndicator(“tag1”)
.setContent(R.id.atablellayout);
tabHost.addTab(spec);//标记1
spec=tabHost.newTabSpec(“tag2”).setIndicator(“tag2”)
.setContent(R.id.anotherTableLayout);
tabHost.addTab(spec);//标记2
FrameLayout=tabHost.getTabContentView();
TableLayout=(TableLayout)frameLayout.findViewById(R.id.atablellayout);
/*创建要添加的新行*/
TableRow TableRow=新的TableRow(本);
/*创建一个按钮作为行内容*/
按钮按钮=新按钮(此按钮);
按钮.setText(“动态按钮”);
按钮。setLayoutParams(新的LayoutParams(
LayoutParams.FILL\u父级,
LayoutParams.WRAP_内容);
/*将按钮添加到行*/
tableRow.addView(按钮);
/*将行添加到TableLayout*/
tableLayout.addView(tableRow,新建tableLayout.LayoutParams(
LayoutParams.FILL\u父级,
LayoutParams.WRAP_内容);
}//onCreate方法的定义结束
}//TabLayoutTest类的定义结束
我没有得到任何回复!嗯,我已经想出了解决办法。解决方案是,在将控件添加到TableRow时,我需要使用TableRow.LayoutParams而不是ViewGroup.LayoutParams
button.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
我没有得到任何回复!嗯,我已经想出了解决办法。解决方案是,在将控件添加到TableRow时,我需要使用TableRow.LayoutParams而不是ViewGroup.LayoutParams
button.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
嗨,这个问题很难回答吗?如果缺少什么,请告诉我,以便我能提供。谢谢。嗨,这个问题很难回答吗?如果缺少什么,请告诉我,以便我能提供。谢谢