1

我制作了一个简单的 tabhost 演示用于学习目的,我已经成功并运行它,但我的问题是我放入可绘制的图像应该像选择器中提到的那样,但它不起作用..它甚至没有显示图像..!我的代码是: 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);


         }

         }

选择器 * 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>

关于.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>
4

1 回答 1

1

你可以试试这个方法。

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

它会很好用。

要么你必须使用这个例子

非常好。

于 2013-06-19T05:19:03.843 回答