因为TabActivity 里本身有个TitleBar,如果Tab里的Activity还想自定义TitleBar的话、会冲突,因为不能同时显示两个TitleBar、、
这里我们要在Tab里的Activity持有TabActivity的引用,这样就能在显示某Activity时动态地自定义标题栏。。
需要注意的是标题栏在TabActivity 里,所以要想找到标题栏的Button等等对象就到通过持有的引用来找了、、、
特别要注意的就是找标题栏对象,得在onResume里自定义标题栏之后,也就是把titlebar layout载入TabActivity之后再找,郁闷了我好久、、、哦哦
查看更多自定义标题栏方法:http://blog.csdn.net/xixinyan/article/details/6931019
遇到的问题:当在不同的activity切换时,或者某activity因为用户输入文字,失去焦点,输入完成后,标题栏的按钮再怎么点击都没反应了。
解决中。。。
package com.test; import android.app.TabActivity; import android.content.Intent; import android.os.Bundle; import android.view.Window; import android.widget.TabHost; public class CustomTitleInTabActivity extends TabActivity { public static CustomTitleInTabActivity mTabActivityInstance; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.main); TabHost tabHost = getTabHost(); // The activity TabHost TabHost.TabSpec spec; // Reusable TabSpec for each tab Intent tabIntent; // Reusable Intent for each tab mTabActivityInstance = this; // Create an Intent to launch an Activity for the tab (to be reused) tabIntent = new Intent().setClass(this, TabA.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("pickrideTab").setIndicator("搭x车") .setContent(tabIntent); tabHost.addTab(spec); // Create an Intent to launch an Activity for the tab (to be reused) tabIntent = new Intent().setClass(this, TabB.class); // Initialize a TabSpec for each tab and add it to the TabHost spec = tabHost.newTabSpec("pickrideTab").setIndicator("搭3车") .setContent(tabIntent); tabHost.addTab(spec); tabHost.setCurrentTab(0); } }
package com.test; import android.app.Activity; import android.os.Bundle; import android.view.Window; public class TabA extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.titilebar_talklist); } @Override protected void onResume() { super.onResume(); CustomTitleInTabActivity.mTabActivityInstance.getWindow() .setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titilebar_talklist); } }
package com.test; import android.app.Activity; import android.os.Bundle; import android.view.Window; public class TabB extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); setContentView(R.layout.titilebar_talklist); } @Override protected void onResume() { super.onResume(); CustomTitleInTabActivity.mTabActivityInstance.getWindow() .setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titilebar_talklist2); } }