这是我的 TABS 目前的样子:
对于每个单独的 TAB,我将如何更改用于显示文本的 TextView 的文本?
如您所见,每个 TAB 的文本都是相同的。我曾尝试使用更改每个单独文本的方法,但似乎没有任何效果。
任何指导将不胜感激。
用于显示 TextView 的 XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/Tab_Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ImageView
android:id="@+id/tab_icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
班级:
public class MainActivity extends FragmentActivity {
/* Tab identifiers */
private final String TAB_A = "Appointments";
private final String TAB_B = "Calender";
private final String TAB_C = "Profile";
private final String TAB_D = "Settings";
private String selectedTab;
private static TabHost mTabHost;
private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
fragment4 = new Fragment4();
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setOnTabChangedListener(listener);
mTabHost.setup();
initializeTab();
}
// TABS
public void initializeTab() {
TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
mTabHost.setCurrentTab(0);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
//First TAB
spec.setIndicator(createTabView(TAB_A, R.drawable.tab1_selector));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(TAB_B);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
//Second TAB
spec.setIndicator(createTabView(TAB_B, R.drawable.tab2_selector));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(TAB_C);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
//Third TAB
spec.setIndicator(createTabView(TAB_C, R.drawable.tab3_selector));
mTabHost.addTab(spec);
spec = mTabHost.newTabSpec(TAB_D);
spec.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return findViewById(android.R.id.tabcontent);
}
});
//Fourth TAB
spec.setIndicator(createTabView("TAB_D", R.drawable.tab4_selector));
mTabHost.addTab(spec);
spec.setIndicator("Tab4");
mTabHost.setCurrentTab(0);
}
TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
public void onTabChanged(String tabId) {
/* Set current tab.. */
if (tabId.equals(TAB_A)) {
pushFragments(tabId, fragment1);
selectedTab = TAB_A;
} else if (tabId.equals(TAB_B)) {
pushFragments(tabId, fragment2);
selectedTab = TAB_B;
} else if (tabId.equals(TAB_C)) {
pushFragments(tabId, fragment3);
selectedTab = TAB_C;
} else if (tabId.equals(TAB_D)) {
pushFragments(tabId, fragment4);
selectedTab = TAB_D;
}
}
};
/*
* adds the fragment to the FrameLayout
*/
public void pushFragments(String tag, Fragment fragment) {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction ft = manager.beginTransaction();
ft.replace(android.R.id.tabcontent, fragment);
ft.commit();
}
/*
* returns the tab view i.e. the tab icon and text
*/
private View createTabView(final String tabText, final int id) {
View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
//Tab image
ImageView imageViewTabIcon = (ImageView) view
.findViewById(R.id.tab_icon);
imageViewTabIcon.setImageDrawable(getResources().getDrawable(id));
//Tab text
TextView TextViewTAB = (TextView) findViewById(R.id.Tab_Text);
return view;
}
public static void tabfresh() {
mTabHost.setCurrentTab(0);
}
public static void tabfresh_sal() {
mTabHost.setCurrentTab(1);
}
}
@kalyan pvs 的崩溃日志
11-04 11:29:36.139: E/AndroidRuntime(18623): FATAL EXCEPTION: main
11-04 11:29:36.139: E/AndroidRuntime(18623): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.question.question/com.question.question.MainActivity}: java.lang.NullPointerException
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.ActivityThread.access$600(ActivityThread.java:138)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1207)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.os.Looper.loop(Looper.java:213)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.ActivityThread.main(ActivityThread.java:4787)
11-04 11:29:36.139: E/AndroidRuntime(18623): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:29:36.139: E/AndroidRuntime(18623): at java.lang.reflect.Method.invoke(Method.java:511)
11-04 11:29:36.139: E/AndroidRuntime(18623): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-04 11:29:36.139: E/AndroidRuntime(18623): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-04 11:29:36.139: E/AndroidRuntime(18623): at dalvik.system.NativeStart.main(Native Method)
11-04 11:29:36.139: E/AndroidRuntime(18623): Caused by: java.lang.NullPointerException
11-04 11:29:36.139: E/AndroidRuntime(18623): at com.question.question.MainActivity.createTabView(MainActivity.java:165)
11-04 11:29:36.139: E/AndroidRuntime(18623): at com.question.question.MainActivity.initializeTab(MainActivity.java:70)
11-04 11:29:36.139: E/AndroidRuntime(18623): at com.question.question.MainActivity.onCreate(MainActivity.java:51)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.Activity.performCreate(Activity.java:5008)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-04 11:29:36.139: E/AndroidRuntime(18623): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2035)
11-04 11:29:36.139: E/AndroidRuntime(18623): ... 11 more