android 选项卡 TabHost的问题

joker_zhou 2012-07-23 06:52:14
我在从第一个卡切换到第二个卡为什么第一卡里面的内容还在
java代码
	public class MainActivity extends TabActivity  {
TabHost tabs =null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs =getTabHost();
LayoutInflater.from(this).inflate(R.layout.a, tabs.getTabContentView(),true);
//设置Tab1
TabSpec tab1 = tabs.newTabSpec("tab1");
tab1.setIndicator("tab1"); // 设置tab1的名称
tab1.setContent(R.id.sa); // 关联控件
tabs.addTab(tab1); // 添加tab1
//设置Tab1
TabSpec tab2 = tabs.newTabSpec("tab1");
tab2.setIndicator("tab1"); // 设置tab1的名称
// tab2.setContent(new Intent(this,LoginActivity.class)); // 关联控件
tab2.setContent(R.id.sas);
tabs.addTab(tab2);
}


xml代码:
activity_main.xml
<?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"
>

<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
>

<LinearLayout android:id="@+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"/>
</LinearLayout>
</TabHost>

a.xml的内容
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/sa"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>



<LinearLayout
android:id="@+id/sas"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />

</LinearLayout>

</LinearLayout>
...全文
97 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
joker_zhou 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

2个tab的名称一模一样?

TabSpec tab2 = tabs.newTabSpec("tab1");
tab2.setIndicator("tab1");

改为

TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator("tab2");

……
[/Quote]
看错了..其实没错..结贴..囧
AMinfo 2012-07-23
  • 打赏
  • 举报
回复
2个tab的名称一模一样?

TabSpec tab2 = tabs.newTabSpec("tab1");
tab2.setIndicator("tab1");

改为

TabSpec tab2 = tabs.newTabSpec("tab2");
tab2.setIndicator("tab2");

试试

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧