我的标签小部件不显示图片图标

时间:2012-11-23 09:04:55

标签: java android eclipse tabs widget

我正在尝试设置应用,我对自己的进步感到满意。我已经设置了一个标签小部件,如下所示,它工作正常,但我设置的图片没有显示。这让我很困惑![在这里输入图像描述] [1]所有正确的代码。任何评论都将大大恢复:)

所以要查看我试图在每个标签下面放置图标,例如收藏夹下的星星和警报下的时钟等......

TrainMain.java

package com.tris.trainbuzzer;

import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

@SuppressWarnings({ "deprecation" })
public class TrainMain extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_main);

        Resources res = getResources();
        TabHost tabHost = getTabHost();

        // Tab for planner
        TabSpec plannerspec = tabHost.newTabSpec("Planner");
        plannerspec.setIndicator("Planner",
                res.getDrawable(R.drawable.icon_planner_tab));
        Intent plannerIntent = new Intent(this, PlannerActivity.class);
        plannerspec.setContent(plannerIntent);

        // Tab for alerts
        TabSpec alertsspec = tabHost.newTabSpec("Alerts");
        // setting Title and Icon for the Tab
        alertsspec.setIndicator("Alerts",
                res.getDrawable(R.drawable.icon_alerts_tab));
        Intent alertsIntent = new Intent(this, AlertsActivity.class);
        alertsspec.setContent(alertsIntent);

        // Tab for settings
        TabSpec settingsspec = tabHost.newTabSpec("Settings");
        settingsspec.setIndicator("Settings",
                res.getDrawable(R.drawable.icon_settings_tab));
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        settingsspec.setContent(settingsIntent);

        // Tab for favourites
        TabSpec favouritesspec = tabHost.newTabSpec("Favourites");
        favouritesspec.setIndicator("Favourites",
                res.getDrawable(R.drawable.icon_favourites_tab));
        Intent favouritesIntent = new Intent(this, FavouritesActivity.class);
        favouritesspec.setContent(favouritesIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(plannerspec); // Adding planner tab
        tabHost.addTab(favouritesspec); // Adding favourites tab
        tabHost.addTab(alertsspec); // Adding alerts tab
        tabHost.addTab(settingsspec); // Adding settings tab



    }
}

activity_train_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>

</TabHost>

icon_planner_tab.xml(在可绘制文件中)

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

   <!-- When selected, use grey -->
    <item android:state_selected="true"
        android:drawable="@drawable/planner_gray" ></item>
   <!-- When selected, use white -->
    <item 
        android:drawable="@drawable/planner_white"></item>

</selector>

1 个答案:

答案 0 :(得分:2)

我测试了你的应用。似乎你在Android ICS或更高版本上运行它。 见差异:

On Android 2.3

On Android 4.0.4

对于自定义标签视图指示器:

private View createTabView(final Context context, final int textStringId, final int imageResId) {
        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
        ImageView iv = (ImageView) view.findViewById(R.id.tabsIcon);
        iv.setImageResource(imageResId);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(textStringId);
        return view;
    }