不少人发现 用完 tabhost 后 当在布局中的一个buton中启动另一个activity 就会发现没有tab 卡片 也就是说他不在tabhost中启动的Activity 而是另起一页 可以这么理解吧 总之
让人很头疼今天就介绍一个解决方案 解决这个办法
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.view.Menu;
import android.widget.TabHost;
public class MainActivity extends TabActivity {
TabHost TabHost;
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabHost = getTabHost();
TabHost.addTab(TabHost
.newTabSpec("t1")
.setIndicator("美女",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(new Intent(this, FirstGroupTab.class)));
TabHost.addTab(TabHost
.newTabSpec("t1")
.setIndicator("天使",
getResources().getDrawable(R.drawable.ic_launcher))
.setContent(new Intent(this, meinv24.class)));
TabHost.setCurrentTab(0);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
第二个类
import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
//第一个标签页显示的Activity,继承ActivityGroup,管理所有子Activity
public class FirstGroupTab extends ActivityGroup {
//用于管理本Group中的所有Activity
public static ActivityGroup group;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
group = this;
}
@Override
public void onBackPressed() {
//把后退事件交给子Activity处理
group.getLocalActivityManager().getCurrentActivity().onBackPressed();
}
@Override
protected void onStart() {
super.onStart();
//要跳转的Activity
Intent intent = new Intent(this, FirstActivity.class);
// 把Activity转换成一个Window,然后转换成View
Window w = group.getLocalActivityManager().startActivity(
"FirstActivity", intent);
View view = w.getDecorView();
//设置要跳转的Activity显示为本ActivityGroup的内容
group.setContentView(view);
}
}
第三个类
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.s1);
//跳转到第二个Activity
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//要跳转的Activity
Intent intent = new Intent(FirstActivity.this, zhongjiezhe_3.class);
// 把Activity转换成一个Window,然后转换成View
Window w = FirstGroupTab.group.getLocalActivityManager()
.startActivity("inv",intent);
View view = w.getDecorView();
//设置要跳转的Activity显示为本ActivityGroup的内容
FirstGroupTab.group.setContentView(view);
}
});
}
}
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="我跳" />
</LinearLayout>
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class zhongjiezhe_3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.in);
Button bu = (Button) findViewById(R.id.busdcdtton1);
bu.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(zhongjiezhe_3.this,
FirstActivity.class);
// 把aCTIVITY转换成window 然后转换成view
Window w = FirstGroupTab.group.getLocalActivityManager()
.startActivity("zhongjiezhe_3", intent);
View view = w.getDecorView();
// 设置要跳转的Activity显示为本ActivityGroup的内容
FirstGroupTab.group.setContentView(view);
}
});
}
}
xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/busdcdtton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RatingBar
android:id="@+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<ToggleButton
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton" />
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chronometer" />
<AnalogClock
android:id="@+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>