我必须为 tabhost 的每个 tabspec 放置一个图标(里面有片段);但是对于这段代码它不起作用:tabspecs 填充视图并且不显示片段。我尝试使用 tSpecCredit.setIndicator("Contatti", getResources().getDrawable(R.drawable.credit)); 但它不适用于 android >=4.0
任何人都可以帮忙吗?非常感谢
活动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="match_parent"
android:layout_height="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
<FrameLayout
android:id="@+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
</TabHost>
一段代码:
tabh = (TabHost) findViewById(android.R.id.tabhost);
tabh.setup();
/** Defining Tab Change Listener event. This is invoked when tab is changed */
OnTabChangeListener tabChangeListener = new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
FragmentManager fm = getSupportFragmentManager();
FragmentWork workFragment = (FragmentWork) fm.findFragmentByTag("work");
FragmentCredit credFragment = (FragmentCredit)fm.findFragmentByTag("credit");
FragmentTransaction ft= fm.beginTransaction();
/** Detaches the fragmentwork if exists */
if(workFragment!=null)
ft.detach(workFragment);
/** Detaches the fragmentcredit if exists */
if(credFragment!=null)
ft.detach(credFragment);
//TODO switch
/** If current tab is work */
if(tabId.equalsIgnoreCase("work")){
if(workFragment==null){
/** Create FragmentWork and adding to fragmenttransaction */
FragmentWork fwork= new FragmentWork();
fwork.setLists(offerWork, formationWork);
ft.add(R.id.realtabcontent ,fwork, "work");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(workFragment);
}
} /** If current tab is credit */
else {
if(credFragment==null){
/** Create FragmentCredit and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new FragmentCredit(), "credit");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(credFragment);
}
}
ft.commit();
}
};
/** Setting tabchangelistener for the tab */
tabh.setOnTabChangedListener(tabChangeListener);
/** Defining tab builder for Work tab */
TabSpec tSpecWork = tabh.newTabSpec("work");
View tabIndicator
=LayoutInflater.from(this).inflate(R.layout.tab_indicator,tabh.getTabWidget(),false);
((TextView) tabIndicator.findViewById(R.id.title_tab)).setText("CARRIERA");
((ImageView) tabIndicator.findViewById(R.id.icon_tab)).setImageResource(R.drawable.work);
tSpecWork.setIndicator(tabIndicator);
tSpecWork.setContent(new EasyTabContent(getBaseContext()));
tabh.addTab(tSpecWork);
/** Defining tab builder for Credit tab */
TabSpec tSpecCredit = tabh.newTabSpec("credit");
View tabIndicatorC =
LayoutInflater.from(this).inflate(R.layout.tab_indicator,tabh.getTabWidget() ,false);
((TextView) tabIndicatorC.findViewById(R.id.title_tab)).setText("CONTATTI");
((ImageView) tabIndicatorC.findViewById(R.id.icon_tab)).setImageResource(R.drawable.credit);
tSpecCredit.setIndicator(tabIndicatorC);
tSpecCredit.setContent(new EasyTabContent(getBaseContext()));
tabh.addTab(tSpecCredit);
编辑:这是 tab_indicator 的 xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="1"
android:padding="5dp" >
<ImageView
android:id="@+id/icon_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/title_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:textColor="#000000" />
</RelativeLayout>