Android 选择器在 tabhost 中不起作用

标签 android android-tabhost android-selector

我已经制作了一个简单的 tabhost 演示用于学习目的,我已经成功制作并运行了它,但我的问题是我放入 drawable 的图像应该像选择器中提到的那样出现,但它不起作用..它不甚至显示图像..!我的代码是: ma​​in.java

package com.example.tabhostdemo;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.view.Menu;
import android.widget.TabHost;


public class TabHostActivity extends TabActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tab_host);
         Resources res = getResources(); // Resource object to get Drawables
         TabHost tabHost = getTabHost(); // The activity TabHost
         TabHost.TabSpec spec; // Reusable TabSpec for each tab
         Intent intent; // Reusable Intent for each tab

         // Create an Intent to launch an Activity for the tab (to be reused)
         intent = new Intent().setClass(this, HomeActivity.class);
         spec = tabHost.newTabSpec("home")
         .setIndicator("HOME", res.getDrawable(R.drawable.home))
         .setContent(intent);
         tabHost.addTab(spec);

         // Do the same for the other tabs

         intent = new Intent().setClass(this, AboutActivity.class);
         spec = tabHost.newTabSpec("about")
         .setIndicator("ABOUT", res.getDrawable(R.drawable.about))
         .setContent(intent);
         tabHost.addTab(spec);


         intent = new Intent().setClass(this, ContactActivity.class);
         spec = tabHost
         .newTabSpec("contact")
         .setIndicator("CONTACT",
         res.getDrawable(R.drawable.contact))
         .setContent(intent);
         tabHost.addTab(spec);

         //set tab which one you want open first time 0 or 1 or 2
         tabHost.setCurrentTab(0);


         }

         }

选择器 *home.xml*

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/home1"
          android:state_selected="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/home2" />
</selector>

联系

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/contact1"
          android:state_selected="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/contact2" />
</selector>

about.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- When selected -->
    <item android:drawable="@drawable/about1"
          android:state_selected="true" />
    <!-- When not selected-->
    <item android:drawable="@drawable/about2" />
</selector>

最佳答案

你可以这样试试。

spec.setIndicator("HOME", setImageResource(R.drawable.home))

它会很好用。

要么你必须使用这个example .

真的很棒。

关于Android 选择器在 tabhost 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17183073/

相关文章:

android - 如何设置文字的颜色和大小? (我使用选择器)

java - 在平板电脑上滚动列表后意外重用了选定的列表项背景颜色

android - 在外部单击时保持通知区域打开

android - 如何检索当前为微调器设置的数组适配器?安卓操作系统,机器人

android - 使用 Javacv 编程 android 项目出现异常

android - 当我点击标签主机时标签内容没有改变

移动网站的安卓应用程序

android - ListView 一次只显示一行

android - 如何在左侧制作tabWidget