求教:不使用xml如何创建TabActivity?多谢! rt 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 TabHost tabHost; public void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); tabHost = getTabHost(); TabSpec spec; spec=tabHost.newTabSpec("Tab1") .setIndicator("Tab1") .setContent(R.id. ); tabHost.addTab(spec); public class tab1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); TextView tv = new TextView(this); tv.setText("tab2"); this.setContentView(tv); }}public class TabHost_3 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(); TabSpec ts; Intent intent; intent = new Intent().setClass(this, tab1.class); ts = tabhost.newTabSpec("Tab1").setIndicator("tab1").setContent(intent); tabhost.addTab(ts); }}这是里是用Intent实现的 Activity:import android.app.TabActivity;import android.os.Bundle;import android.widget.TabHost;import android.widget.TabHost.TabSpec;public class Main extends TabActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TabHost th = getTabHost(); TabSpec ts; ts = th.newTabSpec("Tab1").setIndicator("tab1").setContent(R.id.tab1); th.addTab(ts); ts = th.newTabSpec("Tab2").setIndicator("tab2").setContent(R.id.tab2); th.addTab(ts); ts = th.newTabSpec("Tab3").setIndicator("tab3").setContent(R.id.tab3); th.addTab(ts); }}Main:<?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:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:orientation="vertical"> <TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget> <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab1"></LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab2"></LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab3"></LinearLayout> </FrameLayout> </LinearLayout> </TabHost> 谢谢各位,接受silence_dml的答案了。这是回帖中唯一的不使用xml的方法 webview assets 路径问题 点击弹出层如何避免点击到下面层 。 android android 2.3 ffmpeg 文件搜索出错,求助 一个关于SimpleCursorAdapte的问题 android 平台下编译二进制程序报线程函数问题 FrameBuffer分配(菜鸟求助) Spinner 从数据库中读取数据并实现2级互动 请教一个3d图片的问题 怎么找到android下对应的设备 请问如何获取com.android.phone进程id?
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tabHost = getTabHost();
TabSpec spec;
spec=tabHost.newTabSpec("Tab1")
.setIndicator("Tab1")
.setContent(R.id. );
tabHost.addTab(spec);
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("tab2");
this.setContentView(tv);
}}public class TabHost_3 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();
TabSpec ts;
Intent intent;
intent = new Intent().setClass(this, tab1.class);
ts = tabhost.newTabSpec("Tab1").setIndicator("tab1").setContent(intent);
tabhost.addTab(ts);
}
}这是里是用Intent实现的
import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;public class Main extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost th = getTabHost();
TabSpec ts;
ts = th.newTabSpec("Tab1").setIndicator("tab1").setContent(R.id.tab1);
th.addTab(ts);
ts = th.newTabSpec("Tab2").setIndicator("tab2").setContent(R.id.tab2);
th.addTab(ts);
ts = th.newTabSpec("Tab3").setIndicator("tab3").setContent(R.id.tab3);
th.addTab(ts);
}
}Main:
<?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:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="fill_parent" android:orientation="vertical">
<TabWidget android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/tabs"></TabWidget>
<FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@android:id/tabcontent">
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab1"></LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab2"></LinearLayout>
<LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/tab3"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>