选择时选项卡背景发生变化

发布于 2024-12-03 03:46:32 字数 2513 浏览 1 评论 0原文

我需要在不同状态下设置不同的图像作为选项卡背景。我已将一张图像设置为默认背景,但如何在选择选项卡时切换到另一张图像。下面是我的代码。

public class HelloTabWidget extends  TabActivity {            

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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
        TabWidget tw = getTabWidget(); 


        for (int i = 0; i < tw.getChildCount(); i++) { 
                    View v = tw.getChildAt(i); 
                    v.setBackgroundDrawable(getResources().getDrawable 
        (R.drawable.tab_artist)); 
                  } 



        //First tab
        intent = new Intent().setClass(this, FirstActivity.class);    // Initialize a TabSpec for each tab and add it to the TabHost    
        spec = tabHost.newTabSpec("First").setIndicator("First")
        .setContent(intent);    
        tabHost.addTab(spec);
        getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected);

         //Second tab
         intent = new Intent().setClass(this, SecondActivity.class);    // Initialize a TabSpec for each tab and add it to the TabHost    
            spec = tabHost.newTabSpec("Second").setIndicator("Second")
            .setContent(intent);    
            tabHost.addTab(spec);
             getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected);

             //third
             intent = new Intent().setClass(this, ThirdActivity.class);    // Initialize a TabSpec for each tab and add it to the TabHost    
                spec = tabHost.newTabSpec("Third").setIndicator("Third")
                .setContent(intent);    
                tabHost.addTab(spec);
                getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected);

    }
}

/*tab_artist.xml*/

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
<!-- When selected, use grey -->    
<item android:background="@drawable/tabselected"  android:state_selected="true" />    
<!-- When not selected, use white-->    
<item android:background="@drawable/tabunselected" />
</selector>

I need to set different Images as tab background on different states. I have set one image as background for default but how to switch to other one when tab is selected. Below is my code.

public class HelloTabWidget extends  TabActivity {            

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        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
        TabWidget tw = getTabWidget(); 


        for (int i = 0; i < tw.getChildCount(); i++) { 
                    View v = tw.getChildAt(i); 
                    v.setBackgroundDrawable(getResources().getDrawable 
        (R.drawable.tab_artist)); 
                  } 



        //First tab
        intent = new Intent().setClass(this, FirstActivity.class);    // Initialize a TabSpec for each tab and add it to the TabHost    
        spec = tabHost.newTabSpec("First").setIndicator("First")
        .setContent(intent);    
        tabHost.addTab(spec);
        getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected);

         //Second tab
         intent = new Intent().setClass(this, SecondActivity.class);    // Initialize a TabSpec for each tab and add it to the TabHost    
            spec = tabHost.newTabSpec("Second").setIndicator("Second")
            .setContent(intent);    
            tabHost.addTab(spec);
             getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected);

             //third
             intent = new Intent().setClass(this, ThirdActivity.class);    // Initialize a TabSpec for each tab and add it to the TabHost    
                spec = tabHost.newTabSpec("Third").setIndicator("Third")
                .setContent(intent);    
                tabHost.addTab(spec);
                getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected);

    }
}

/*tab_artist.xml*/

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
<!-- When selected, use grey -->    
<item android:background="@drawable/tabselected"  android:state_selected="true" />    
<!-- When not selected, use white-->    
<item android:background="@drawable/tabunselected" />
</selector>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

过度放纵 2024-12-10 03:46:32
public class HelloTabWidget extends  TabActivity implements OnTabChangeListener{  
.....

mTabHost. setOnTabChangedListener(this);

@Override
    public void onTabChanged(String tabId) {

              // Here in tabId you will get the name of the Tab from that you can check and set the background 
                // of the requirement tab according to need.
    }
}
public class HelloTabWidget extends  TabActivity implements OnTabChangeListener{  
.....

mTabHost. setOnTabChangedListener(this);

@Override
    public void onTabChanged(String tabId) {

              // Here in tabId you will get the name of the Tab from that you can check and set the background 
                // of the requirement tab according to need.
    }
}
没企图 2024-12-10 03:46:32

实现 onTabChangeListener() 并修改它们的背景。干杯

http://developer.android.com/reference/android/widget/ TabHost.OnTabChangeListener.html

编辑:
使用 tabHost 来实现该方法。您可以在任何您想要的地方实施。假设在设置所有 TabWidget 后执行此操作。最好的做法是使用选项卡的 id,就像将它们设置为“第一”、“第二”等。

 tabHost.setOnTabChangedListener(new OnTabChangeListener(){
          @Override
            public void onTabChanged(String tabId) {
                        if(tabId.equals("First"){
                                //do something
                        }else if(tabId.equals("Second"))
                        {
                            //do something
                        }// etc etc etc


                        }});

Implement onTabChangeListener() and there modify their backgrounds. Cheers

http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html

Edit:
Use the tabHost to implement the method. You can implemented where ever you want. Let's say do it after you set all the TabWidgets. Its good practice to use ids of the tab like you've set them "First", "Second" etc etc.

 tabHost.setOnTabChangedListener(new OnTabChangeListener(){
          @Override
            public void onTabChanged(String tabId) {
                        if(tabId.equals("First"){
                                //do something
                        }else if(tabId.equals("Second"))
                        {
                            //do something
                        }// etc etc etc


                        }});
孤千羽 2024-12-10 03:46:32

这可能对你有帮助

Tabhost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
        public void onTabChanged(String tabId) {

            for(int i=0;i<tb.getTabWidget().getChildCount();i++)
            {
                   tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected
            }
            tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected); 
}});

this may help you

Tabhost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
        public void onTabChanged(String tabId) {

            for(int i=0;i<tb.getTabWidget().getChildCount();i++)
            {
                   tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected
            }
            tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected); 
}});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文