Android如何更改选项卡上的选项卡图标

时间:2012-03-16 20:57:31

标签: android android-layout android-widget

机器人:

我是android的新手我想更改标签点击上的标签图标。我已经按照教程指导如何通过selecter xml文件(如

)更改标签图标

http://developer.android.com/resources/tutorials/views/hello-tabwidget.html http://www.androidhive.info/2011/08/android-tab-layout-tutorial/

但是在我的原因中为什么所有这些都不起作用它只更改了第一个选中的选项卡图标未选中和未选中选择但不适用于所有其他我的代码中的问题请帮助我plz plz plz

我的代码如下

我的可绘制图标设置文件如下所示,名称为

文件夹

下的becel_setting.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- When selected, -->
    <item android:drawable="@drawable/menu_promenera_selected"
          android:state_selected="true"
          android:state_pressed="false" />

    <!-- When not selected, -->
    <item android:drawable="@drawable/menu_promenera"
         />

    <!-- When selected, -->
    <item android:drawable="@drawable/menu_varaframsteg_selected"
          android:state_selected="true"
          android:state_pressed="false" />
    <!-- When not selected, -->
    <item android:drawable="@drawable/menu_varaframsteg"
         />

    <!-- When selected,  -->
    <item android:drawable="@drawable/menu_minapromenader_selected"
          android:state_selected="true"
          android:state_pressed="false" />
    <!-- When not selected,-->
    <item android:drawable="@drawable/menu_minapromenader"
         />

    <!-- When selected,  -->
    <item android:drawable="@drawable/menu_information_selected"
          android:state_selected="true"
          android:state_pressed="false" />
    <!-- When not selected, -->
    <item android:drawable="@drawable/menu_information"
         />

</selector>

我的标签主机主机类如下

public class BECEL extends TabActivity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try{
            setMenuTabs();
        }catch(Exception ex){
            String message = ex.getMessage();
        }
    }

    private void setMenuTabs() {
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        /*******************************************/

        intent = new Intent(this, BecelFacebookConnection.class);

        spec = tabHost.newTabSpec("").setIndicator("",
        res.getDrawable(R.drawable.becel_setting)).setContent(intent);  /**** xml file name becel_setting ******/
        tabHost.addTab(spec);        



        intent = new Intent(this, OurProgress.class);
        Drawable mySelector = getResources().getDrawable(R.drawable.menu_varaframsteg);
        spec = tabHost.newTabSpec("").setIndicator("",mySelector).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, MyWalks.class);

        spec = tabHost.newTabSpec("").setIndicator("",
        res.getDrawable(R.drawable.menu_minapromenader)).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, WalkInformations.class);

        spec = tabHost.newTabSpec("").setIndicator("",
        res.getDrawable(R.drawable.menu_information)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}

请帮我解决我的代码中的问题

1 个答案:

答案 0 :(得分:1)

您需要为每个 newTabSpec(“”)提供唯一的字符串值。例如,以下示例中的 tab1 tab2 tab3 tab4

public class BECEL extends TabActivity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try{
            setMenuTabs();
        }catch(Exception ex){
            String message = ex.getMessage();
        }
    }

    private void setMenuTabs() {
        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost

        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        /*******************************************/

        intent = new Intent(this, BecelFacebookConnection.class);

        spec = tabHost.newTabSpec("tab1").setIndicator("tab1",
        res.getDrawable(R.drawable.becel_setting)).setContent(intent);  /**** xml file name becel_setting ******/
        tabHost.addTab(spec);        



        intent = new Intent(this, OurProgress.class);
        Drawable mySelector = getResources().getDrawable(R.drawable.menu_varaframsteg);
        spec = tabHost.newTabSpec("tab2").setIndicator("tab2",mySelector).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, MyWalks.class);

        spec = tabHost.newTabSpec("tab3").setIndicator("tab3",
        res.getDrawable(R.drawable.menu_minapromenader)).setContent(intent);
        tabHost.addTab(spec);



        intent = new Intent(this, WalkInformations.class);

        spec = tabHost.newTabSpec("tab4").setIndicator("tab4",
        res.getDrawable(R.drawable.menu_information)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}