Java “我的选项卡”小部件没有';t显示图片图标

Java “我的选项卡”小部件没有';t显示图片图标,java,android,eclipse,tabs,widget,Java,Android,Eclipse,Tabs,Widget,我正在尝试设置一个应用程序,我对自己的进展感到满意。我已经设置了一个选项卡小部件,如下所示,它工作正常,但是我设置的图片没有显示。这是令人困惑的,因为我似乎有![在此输入图像描述][1]所有正确的代码。任何评论都将被大大恢复:) 所以,为了复习,我试着在每个标签下面放上图标,例如“收藏夹”下的一个星星和“提醒”下的一个时钟等等 TrainMain.java package com.tris.trainbuzzer; import android.app.TabActivity; import

我正在尝试设置一个应用程序,我对自己的进展感到满意。我已经设置了一个选项卡小部件,如下所示,它工作正常,但是我设置的图片没有显示。这是令人困惑的,因为我似乎有![在此输入图像描述][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



    }
}
活动\u train\u 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(在可绘制文件中)


我测试了你的应用程序。看起来你是在安卓ICS或更高版本上运行的。 见区别:

对于自定义选项卡视图指示器:

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;
    }

您是否将图像保存在同名的mdpi、hdpi、xhdpi文件夹中?或者您只有一个可绘制文件夹?我在每个文件夹中以正确的像素数保存所有文件。我应该如何运行它?抱歉?谢谢,我在api级别为8的情况下尝试了它,它成功了。然而,这意味着我不能使用我的开关。你能帮我吗?你可以尝试将自定义视图设置为指示器。这是一个代码的小快照:私有视图createTabView(最终上下文上下文,最终int-textStringId,最终int-imageResId){view-view=LayoutFlater.from(上下文)。充气(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);返回视图;}
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;
    }