0

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

1 回答 1

2

我测试了你的应用程序。似乎您在 Android ICS 或更高版本上运行它。查看区别:

在安卓 2.3 上

在安卓 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;
    }
于 2012-11-23T09:47:30.797 回答