My tab widget doesn't display the picture icons

ε祈祈猫儿з 提交于 2019-12-10 11:54:47

问题


I'm trying to set up an app and I am happy with my progress. I've set up a tab widget as seen below which works fine, however the pictures I have set up are not displaying. This is confusing as I seem to have![enter image description here][1] all the right code. Any comments will be greatly revived :)

So to review I am trying to put icons underneath each tab, for example a star under favourites and a clock under alerts etc...

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(in the drawable file)

<?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:


I tested your app. Seems you ran it on Android ICS or higher. See difference:

For custom tab-view-indicator:

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


来源:https://stackoverflow.com/questions/13525800/my-tab-widget-doesnt-display-the-picture-icons

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!