Android selector not working in tabhost

淺唱寂寞╮ 提交于 2019-12-08 05:23:14

问题


i have made a simple tabhost demo for learning purpose,i have made it successfully and running it as well,But my problem is images which i have put in drawable should come as mentioned in selectors but its not working..its not even showing images..!my code is: main.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);


         }

         }

selectors *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>

contact

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

回答1:


You can try this way.

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

It will work great.

Either you have to used this example.

Really great.



来源:https://stackoverflow.com/questions/17183073/android-selector-not-working-in-tabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!