我的标签窗口小部件不显示图片图标 [英] My tab widget doesn't display the picture icons
问题描述
我想建立一个应用程序,我很高兴我的进步。我设置为可见一个标签窗口小部件,低于该作品我已经设置了不显示正常,但是图片。这是令人困惑,因为我似乎有![此处输入的形象描述] [1]所有正确的code。任何意见将大大苏醒)
所以审查我试图把图标每个选项卡下,例如在收藏夹中的明星,在警报时钟等等...
TrainMain.java
包com.tris.trainbuzzer;进口android.app.TabActivity;
进口android.content.Intent;
进口android.content.res.Resources;
进口android.os.Bundle;
进口android.widget.TabHost;
进口android.widget.TabHost.TabSpec;@燮pressWarnings({德precation})
公共类TrainMain扩展TabActivity {
/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_train_main); 资源解析度= getResources();
TabHost tabHost = getTabHost(); //标签了解策划师
则tabspec plannerspec = tabHost.newTabSpec(规划);
plannerspec.setIndicator(规划
res.getDrawable(R.drawable.icon_planner_tab));
意图plannerIntent =新意图(这一点,PlannerActivity.class);
plannerspec.setContent(plannerIntent); //标签警报
则tabspec alertsspec = tabHost.newTabSpec(通知);
//设置标题和图标的选项卡
alertsspec.setIndicator(警报,
res.getDrawable(R.drawable.icon_alerts_tab));
意图alertsIntent =新意图(这一点,AlertsActivity.class);
alertsspec.setContent(alertsIntent); //为标签的设置
则tabspec settingsspec = tabHost.newTabSpec(设置);
settingsspec.setIndicator(设置,
res.getDrawable(R.drawable.icon_settings_tab));
意图settingsIntent =新意图(这一点,SettingsActivity.class);
settingsspec.setContent(settingsIntent); //标签了解收藏
则tabspec favouritesspec = tabHost.newTabSpec(最爱);
favouritesspec.setIndicator(我的最爱,
res.getDrawable(R.drawable.icon_favourites_tab));
意图favouritesIntent =新意图(这一点,FavouritesActivity.class);
favouritesspec.setContent(favouritesIntent); //添加所有要则tabspec TabHost
tabHost.addTab(plannerspec); //添加计划器选项卡
tabHost.addTab(favouritesspec); //添加收藏夹选项卡
tabHost.addTab(alertsspec); //添加警报选项卡
tabHost.addTab(settingsspec); //添加设置选项卡 }
}
activity_train_main.xml
<?XML版本=1.0编码=UTF-8&GT?;
< TabHost的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@机器人:ID / tabhost
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT> <的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直> < TabWidget
机器人:ID =@机器人:ID /标签
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT/> <的FrameLayout
机器人:ID =@机器人:ID / tabcontent
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>
< / LinearLayout中>< / TabHost>
icon_planner_tab.xml(在绘文件)
<?XML版本=1.0编码=UTF-8&GT?;
<选择的xmlns:机器人=http://schemas.android.com/apk/res/android> <! - 当选择时,使用灰色 - >
<项目的android:state_selected =真
机器人:可绘制=@绘制/ planner_gray>< /项目>
<! - 当选择时,用白 - >
<项目
机器人:可绘制=@绘制/ planner_white>< /项目>< /选择>
我测试您的应用程序。看来你运行它在Android ICS或更高。
见区别:
有关自定义选项卡 - 视图 - 指示器:
私人浏览createTabView(最终上下文的背景下,最终诠释textStringId,最终诠释imageResId){
查看查看= LayoutInflater.from(上下文).inflate(R.layout.tabs_bg,NULL);
ImageView的IV =(ImageView的)view.findViewById(R.id.tabsIcon);
iv.setImageResource(imageResId);
TextView的电视=(TextView中)view.findViewById(R.id.tabsText);
tv.setText(textStringId);
返回视图。
}
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>
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;
}
这篇关于我的标签窗口小部件不显示图片图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!