Android的选择在tabhost不工作 [英] Android selector not working in tabhost

查看:118
本文介绍了Android的选择在tabhost不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为学习目的,一个简单的演示tabhost,我已成功并运行它为好,但我的问题是我已经把绘应选择来作为图像提及,但它不是working..its不!甚至呈现图像..我的code是:
main.java

 包com.example.tabhostdemo;进口android.os.Bundle;
进口android.app.Activity;
进口android.app.TabActivity;
进口android.content.Intent;
进口android.content.res.Resources;
进口android.view.Menu;
进口android.widget.TabHost;
公共类TabHostActivity扩展TabActivity {
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_tab_host);
         资源解析度= getResources(); //资源对象获取可绘制
         TabHost tabHost = getTabHost(); //活动TabHost
         TabHost.TabSpec规范; //可重复使用的则tabspec为每个标签
         意图意图; //可重用的意图为每个标签         //创建一个Intent来启动一个活动的标签(重复使用)
         意图=新意图()setClass(这一点,HomeActivity.class)。
         规格= tabHost.newTabSpec(家)
         .setIndicator(HOME,res.getDrawable(R.drawable.home))
         .setContent(意向);
         tabHost.addTab(规范);         //做其他选项卡相同         意图=新意图()setClass(这一点,AboutActivity.class)。
         规格= tabHost.newTabSpec(关于)
         .setIndicator(关于,res.getDrawable(R.drawable.about))
         .setContent(意向);
         tabHost.addTab(规范);
         意图=新意图()setClass(这一点,ContactActivity.class)。
         规格= tabHost
         .newTabSpec(接触)
         .setIndicator(联系,
         res.getDrawable(R.drawable.contact))
         .setContent(意向);
         tabHost.addTab(规范);         //要打开第一次0或1或2的其中一个设置制表
         tabHost.setCurrentTab(0);
         }         }

选择
*的 home.xml 的*

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <! - 当选择 - >
    <项目机器人:可绘制=@绘制/和home1
          机器人:state_selected =真/>
    <! - 如果不选择 - >
    <项目机器人:可绘制=@绘制/ HOME2/>
< /选择>

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <! - 当选择 - >
    <项目机器人:可绘制=@绘制/ contact1
          机器人:state_selected =真/>
    <! - 如果不选择 - >
    <项目机器人:可绘制=@绘制/ contact2/>
< /选择>

about.xml

 <?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <! - 当选择 - >
    <项目机器人:可绘制=@绘制/ about1
          机器人:state_selected =真/>
    <! - 如果不选择 - >
    <项目机器人:可绘制=@绘制/ about2/>
< /选择>


解决方案

您可以试试这个方法。

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

这会是不错的。

要么你必须使用该例如

真是太好了。

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>

解决方案

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.

这篇关于Android的选择在tabhost不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆