如何更改TabHost中的选项卡图像

我在我的应用程序中使用TabHost,我在我的应用程序中使用了四个选项卡,当我选择特定选项卡而未选中时,我想在TabHost中使用不同的图像。我需要为每个特定标签使用不同的图像。 当我选择任何标签时,图像有点亮,当我切换到另一个标签时,明亮的图像变为灰色阴影。 我已经实现了TabHost,但我不知道如何更改TabHost中的图像。 任何人都可以帮助我。 谢谢, 大卫     
已邀请:
如果您希望为选定和未选择状态使用不同的图像,则在drawables文件夹中为每个选项卡创建“选择器”XML文件,例如tab1_selector.xml,tab2_selector.xml,其中应包含以下内容,将所选图像和未选定状态的可绘制引用替换为图像。即
    <?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_selected="true"
    android:drawable="@drawable/tab1_selected_image" />
  <item
    android:state_selected="false"
    android:drawable="@drawable/tab2_unselected_image" />
</selector>
然后使用.setIndicator方法作为上面写的bharath,你应该引用你的新xml drawable资源。     
首先,您必须拥有这两个图像,因为您想要从一个图像更改为另一个图像,因此您需要这两个图像,并且必须将它放在三个可绘制文件夹中。 在我的例子中,我需要一个名为icon1.png和icon2.png的图像。 之后,在drawable文件夹中创建一个xml文件(所有可绘制文件夹的文件相同)。这是文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use icon1 -->
<item android:drawable="@drawable/icon1"
      android:state_selected="true" />
<!-- When not selected, use icon2-->
<item android:drawable="@drawable/icon2" />
</selector>
您可以选择选择选项卡时将显示的图像。在这种情况下,会出现icon1,因为我们在标签上声明了state_selected = true。 现在,您在三个可绘制文件夹中有两个图像和xml文件。好! 现在,在类中声明选项卡,为要添加的每个选项卡添加此行。
tabHost.addTab(tabHost
.newTabSpec("one")
.setIndicator("The Tab",
  res.getDrawable(R.drawable.yourxmlfile))
.setContent(new Intent(this, YourClass.class)));
请记住,R.drawable.yourxmlfile与您在可绘制文件夹中创建的xml文件相对应。 而已!希望这对你有所帮助。     
设置文字和图标我们需要使用setIndicator属性。
 tabSpec.setIndicator(Char,Drawable);
 firstTabSpec.setIndicator("First Tab Name", getResources().getDrawable(R.drawable.logo));
 secondTabSpec.setIndicator("Second Tab Name",getResources().getDrawable(R.drawable.logo));
使用此选项为每个选项卡设置单独的图像     
创建一个选择器xml文件tabicon.xml并放入此代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_enbled" android:state_selected="true"/>
    <item android:drawable="@drawable/tab_default" android:state_selected="false"/>
</selector>
现在转到TabActivity并输入此代码
TabSpec MyTab = tabhost.newTabSpec("MyTab");
MyTab.setIndicator("", getResources().getDrawable(R.drawable.tabicon));
//note:if you give some text in setIndicator sometimes the icon will not be showed. 
Intent tabIntent = new Intent(this, TabOne.class);
TWTTab.setContent(tabIntent);
    
在此TabLayout教程中,选择选项卡但未选中选项卡时将使用不同的图像。 基本上你必须创建一个Statelist drawable。这是开发者网站的相同代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected, use grey -->
    <item android:drawable="@drawable/ic_tab_artists_grey"
          android:state_selected="true" />
    <!-- When not selected, use white-->
    <item android:drawable="@drawable/ic_tab_artists_white" />
</selector>
还调用setIndicator(CharSequence,Drawable)来设置选项卡的文本和图标。     
此代码显示如何在选项卡主机中设置图标以及设置意图
  TabHost tabHost = getTabHost();

        // Tab for Photos
        TabSpec photospec = tabHost.newTabSpec("Photos");
        // setting Title and Icon for the Tab
        photospec.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.icon_photos_tab));
        Intent photosIntent = new Intent(this, PhotosActivity.class);
        photospec.setContent(photosIntent);

        // Tab for Songs
        TabSpec songspec = tabHost.newTabSpec("Songs");
        songspec.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.icon_songs_tab));
        Intent songsIntent = new Intent(this, SongsActivity.class);
        songspec.setContent(songsIntent);


        // Tab for Videos
        TabSpec videospec = tabHost.newTabSpec("Videos");
        videospec.setIndicator("", getApplicationContext().getResources().getDrawable(R.drawable.icon_videos_tab));
        Intent videosIntent = new Intent(this, VideosActivity.class);
        videospec.setContent(videosIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(photospec); // Adding photos tab
        tabHost.addTab(songspec); // Adding songs tab
        tabHost.addTab(videospec); // Adding videos tab
    
您可以使用ImageButton更好,因为可以选择而不选择imageView,并且可以选择未选择和按下的ImageButton以及其他....     
@Suchismita更好地使用TextView而不是TabActivity。 我在tabactivity中面临以下问题 我无法在同一个标​​签中开始另一个活动,这是我遇到的主要问题 接下来是自定义选项卡的视图,我无法更改分隔符可绘制。 并且在ICS中不推荐使用TabActivity 接下来使用TextView我发现它非常容易处理事件和活动流程,这里您可以完全控制应用程序的行为,也可以根据需要自定义选项卡的外观。 你对如何实施感兴趣吗?     
如果要以编程方式更改选项卡的图像,则:
ImageView yourImage= (ImageView)mTabHost.getTabWidget().getChildTabViewAt(youtTabPosition).findViewById(android.R.id.icon);   
yourImage.setImageResource(R.drawable.ic_more_vert_white_24dp);
    

要回复问题请先登录注册