这就是我的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
答案 0 :(得分:1)
TextViewTAB.setText(tabText);
//添加到您的代码中
在此方法中,设置各个标签的文本
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)view. findViewById(R.id.Tab_Text); //Added to your code
TextViewTAB.setText(tabText); //Added to your code
return view;
}
tabText
已作为方法
答案 1 :(得分:0)
您正在使用TabHost,其中首先使用它,但您仍然希望使用它来更改 TabActivity
的类的扩展部分修改强>:
带有TabActivity的我的代码根据您的要求更改它:
public class MainActivity extends TabActivity {
/** The m tab host. */
private TabHost mTabHost;
/** The intent. */
Intent intent;
/** The feature. */
short feature = 1;
/** The deal. */
short deal = 2;
/** The bundle. */
Bundle bundle;
/** The my drawable. */
Drawable myDrawable;
/** The active fragment. */
public TabFragment activeFragment;
/** The add deal interface. */
AddDealInterface addDealInterface;
/** The link name. */
String linkName;
/**
* Sets the adds the deal interface.
*
* @param addDealInterface
* the new adds the deal interface
*/
public void setAddDealInterface(AddDealInterface addDealInterface) {
this.addDealInterface = addDealInterface;
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.tab);
String Language = getIntent().getStringExtra("lang");
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
myDrawable = getResources().getDrawable(R.drawable.tabclick1);
bundle = new Bundle();
bundle.putShort("Value", feature);
bundle.putString("lang", Language);
final Intent i = new Intent().setClass(this, MainView.class);
i.putExtra("bundle", bundle);
TabSpec spec1 = mTabHost.newTabSpec(getString(R.string.featured));
spec1.setContent(i);
spec1.setIndicator(createTabView(getApplicationContext(), myDrawable,
getString(R.string.featured)));
myDrawable = getResources().getDrawable(R.drawable.tabclick2);
bundle = new Bundle();
bundle.putShort("Value", deal);
bundle.putString("lang", Language);
final Intent i2 = new Intent().setClass(this, MainView.class);
i2.putExtra("bundle", bundle);
TabSpec spec2 = mTabHost.newTabSpec(getString(R.string.deals));
spec2.setContent(i2);
spec2.setIndicator(createTabView(getApplicationContext(), myDrawable,
getString(R.string.deals)));
myDrawable = getResources().getDrawable(R.drawable.tabclick3);
bundle = new Bundle();
bundle.putString("lang", Language);
final Intent i3 = new Intent().setClass(this, FavoriteView.class);
i3.putExtra("bundle", bundle);
TabSpec spec3 = mTabHost.newTabSpec(getString(R.string.favorite));
spec3.setContent(i3);
spec3.setIndicator(createTabView(getApplicationContext(), myDrawable,
getString(R.string.favorite)));
myDrawable = getResources().getDrawable(R.drawable.tabclick4);
bundle = new Bundle();
bundle.putString("lang", Language);
final Intent i4 = new Intent().setClass(this, Search.class);
i4.putExtra("bundle", bundle);
TabSpec spec4 = mTabHost.newTabSpec(getString(R.string.search));
spec4.setContent(i4);
spec4.setIndicator(createTabView(getApplicationContext(), myDrawable,
getString(R.string.search)));
myDrawable = getResources().getDrawable(R.drawable.tabclick5);
bundle = new Bundle();
bundle.putString("lang", Language);
final Intent i5 = new Intent().setClass(this, SettingView.class);
i5.putExtra("bundle", bundle);
TabSpec spec5 = mTabHost.newTabSpec(getString(R.string.setting));
spec5.setContent(i5);
spec5.setIndicator(createTabView(getApplicationContext(), myDrawable,
getString(R.string.setting)));
mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub
int selectedTab = mTabHost.getCurrentTab();
if (selectedTab == 0) {
myDrawable = getResources().getDrawable(R.drawable.close);
linkName = getString(R.string.featured);
}
if (selectedTab == 1) {
myDrawable = getResources().getDrawable(
R.drawable.ic_action_search);
linkName = getString(R.string.deals);
}
if (selectedTab == 2) {
myDrawable = getResources().getDrawable(R.drawable.setting);
linkName = getString(R.string.favorite);
}
if (selectedTab == 3) {
myDrawable = getResources().getDrawable(R.drawable.setting);
linkName = getString(R.string.search);
}
if (selectedTab == 4) {
myDrawable = getResources().getDrawable(R.drawable.setting);
linkName = getString(R.string.setting);
}
Log.d("SELECTED", "" + selectedTab);
}
});
mTabHost.addTab(spec1);
mTabHost.addTab(spec2);
mTabHost.addTab(spec3);
mTabHost.addTab(spec4);
mTabHost.addTab(spec5);
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onActivityResult(int, int,
* android.content.Intent)
*/
@Override
protected void onActivityResult(int arg0, int arg1, Intent arg2) {
super.onActivityResult(arg0, arg1, arg2);
// AddDeal deal = new AddDeal();
if (addDealInterface != null)
addDealInterface.onRespondedResult(arg0, arg1, arg2);
// Toast.makeText(this, "I am called", Toast.LENGTH_SHORT).show();
}
/**
* Creates the tab view.
*
* @param context
* the context
* @param drawable
* the drawable
* @param text
* the text
* @return the view
*/
public static View createTabView(Context context, Drawable drawable,
String text) {
View view = LayoutInflater.from(context).inflate(R.layout.tabs_main_bg,
null);
TextView textView = (TextView) view.findViewById(R.id.lblTabTag);
ImageView imageView = (ImageView) view.findViewById(R.id.imgTabTag);
imageView.setImageDrawable(drawable);
textView.setText(text);
textView.setTextColor(Color.WHITE);
return view;
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onDestroy()
*/
@Override
protected void onDestroy() {
super.onDestroy();
mTabHost = null;
}
/*
* (non-Javadoc)
*
* @see android.support.v4.app.FragmentActivity#onBackPressed()
*/
@Override
public void onBackPressed() {
activeFragment.onBackPressed();
}
// method for TabFragment to call when the user navigates out of the app
/**
* Close.
*/
public void close() {
super.onBackPressed();
}
}