TabHpec上TabHpec中的图标

时间:2013-05-23 13:13:21

标签: android android-tabhost android-tabs

我必须为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 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>

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

求助:我将tab_indicator的RelativeLayout更改为LinearLayout,一切正常!