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

发布于 2024-10-08 21:30:17 字数 234 浏览 0 评论 0原文

我在应用程序中使用 TabHost,在应用程序中使用四个选项卡,并且当选择特定选项卡和未选择特定选项卡时,我想在 TabHost 中使用不同的图像。我需要为每个特定选项卡使用不同的图像。

当我选择任何选项卡时,图像有点亮,当我切换到另一个选项卡时,明亮的图像会变成灰色阴影。

我已经实现了 TabHost,但我不知道如何更改 TabHost 中的图像。

任何人都可以帮助我吗?

谢谢, 大卫

I am using the TabHost in my application, I am using four Tabs in my application and I want to use the different Images in the TabHost when the Particular Tab is been Selected and not selected. I need to use to different Images for a particular tab each.

When I Select any Tab the Image is little bright and when I switch to another Tab that bright Image becomes grey shaded.

I have implemented the TabHost but I don know how to change the Images in the TabHost.

Can anybody help me in this.

Thanks,
david

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

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

发布评论

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

评论(9

早茶月光 2024-10-15 21:30:17

如果您希望对选定和未选定状态使用不同的图像,请在每个选项卡的可绘制文件夹中创建“选择器”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 可绘制资源。

If you wish to use different images for the selected and unselected states, then create 'selector' XML files in your drawables folder for each tab, e.g. tab1_selector.xml, tab2_selector.xml which should contain the following, replacing the drawable references to your images for selected and unselected states. i.e.

    <?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>

Then using the .setIndicator method as bharath wrote above you should reference your new xml drawable resource.

简美 2024-10-15 21:30:17

首先,您必须拥有两个图像,因为您想要从一个图像更改为另一个图像,所以您需要两个图像,并且必须将其放在三个可绘制文件夹中。

在我的示例中,我必须使用图像,其中一个名为 icon1.pngicon2.png

之后,在可绘制文件夹内创建一个 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 文件。

就是这样!希望这对您有帮助。

First of all you must have the two images, because you want to change from one to another, so you need the both images, and you must place it on the three drawable folders.

In my example I have to images, one called icon1.png and icon2.png.

After that, create a xml file inside the drawable folders (the same file for all drawable folders). This is the file:

<?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>

You can choose what image is the one which will appear when the tab is selected. In this case, the icon1 will appear, cause we declared it on the tag where state_selected=true.

So now, you have the two images and the xml file inside the three drawable folders. Ok!

Now, in the class you declare the tabs, add this line for each tab you want to add.

tabHost.addTab(tabHost
.newTabSpec("one")
.setIndicator("The Tab",
  res.getDrawable(R.drawable.yourxmlfile))
.setContent(new Intent(this, YourClass.class)));

Remember that R.drawable.yourxmlfile correponds to the xml file you created in the drawable folders.

That's it! Hope this helps you.

窗影残 2024-10-15 21:30:17

设置文本和icon 我们需要使用 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));

使用它为每个选项卡设置单独的图像

To set text & icon we need to use setIndicator property.

 tabSpec.setIndicator(Char,Drawable);
 firstTabSpec.setIndicator("First Tab Name", getResources().getDrawable(R.drawable.logo));
 secondTabSpec.setIndicator("Second Tab Name",getResources().getDrawable(R.drawable.logo));

use this to set seperate image for each tab

风苍溪 2024-10-15 21:30:17

创建一个选择器 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);

Create a selector xml file tabicon.xml and put this code

<?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>

now go to your TabActivity and put this code

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);
人生戏 2024-10-15 21:30:17

这个 TabLayout 教程中,当 Tab 被使用时,会使用不同的图像已选择和未选择。

基本上你必须创建一个 Statelist 可绘制对象。这是来自开发人员网站的相同代码,

<?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) 来设置选项卡的文本和图标。

In this TabLayout tutorial, different images are used when a Tab is selected and not selected.

Basically you have to create a Statelist drawable. Here's the code for the same from the developer site

<?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>

Also setIndicator(CharSequence, Drawable) is called to set the text and icon for the tab.

冷血 2024-10-15 21:30:17

此代码显示如何在选项卡主机中设置图标以及设置意图

  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

this codes show how to set an icon in tab host and also setting intent

  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
蓬勃野心 2024-10-15 21:30:17

您可以使用 ImageButton ,它更好,因为 imageView 可以选择和不选择,并且 ImageButton 可以选择不选择和按下等等......

You can to use ImageButton it's better because a imageView can be selected and not selected and the ImageButton can be selected not selected and pressed and others....

流殇 2024-10-15 21:30:17

@Suchismita 最好使用 TextView 而不是 TabActivity。
我在选项卡活动中遇到以下问题

  • 我无法在同一选项卡中启动另一个活动,这是我面临的主要问题

  • 下一步正在自定义选项卡视图,我无法更改可绘制的分隔线。

  • TabActivity 在 ICS

Next 中已弃用,使用 TextView 我发现它非常容易处理事件和活动流,在这里您可以完全控制应用程序的行为,还可以根据需要自定义选项卡的外观和感觉。

您对如何实施感兴趣吗?

@Suchismita better you use TextView instead of TabActivity.
I faced these following problems in tabactivity

  • I could not start another activity within same tab, this is major problem i faced

  • next is customizing view of tab, I could not change divider drawable.

  • And TabActivity is deprecated in ICS

Next using TextView I found its very easy to handle events and activity flow,here You have full control on the behavior of application and also You can customize the look and feel of tab however you want.

are you interested in how to implement ?

撩心不撩汉 2024-10-15 21:30:17

如果您想以编程方式更改选项卡的图像,则:

ImageView yourImage= (ImageView)mTabHost.getTabWidget().getChildTabViewAt(youtTabPosition).findViewById(android.R.id.icon);   
yourImage.setImageResource(R.drawable.ic_more_vert_white_24dp);

If you want to change image of tab programmatically then:

ImageView yourImage= (ImageView)mTabHost.getTabWidget().getChildTabViewAt(youtTabPosition).findViewById(android.R.id.icon);   
yourImage.setImageResource(R.drawable.ic_more_vert_white_24dp);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文