我的标签窗口小部件不显示图片图标 [英] My tab widget doesn't display the picture icons

查看:224
本文介绍了我的标签窗口小部件不显示图片图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个应用程序,我对我的进度感到满意。我已经设置了一个选项卡小部件,如下所示,工作正常,但是我设置的图片没有显示。这是令人困惑的,因为我似乎有![输入图像描述] [1]所有正确的代码。任何意见将会大大复兴:)

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

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

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(在可绘制文件中)

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>


推荐答案

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

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

对于自定义tab-view-indicator:

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

这篇关于我的标签窗口小部件不显示图片图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆