Android 选项卡中的图标未显示
我从Android开始,当我尝试用图标和文本做一个TabHost时。只有文本可见,如果我将文本留空,则可以看到图标。我想在屏幕上看到两者 有人能给我一些建议吗Android 选项卡中的图标未显示,android,Android,我从Android开始,当我尝试用图标和文本做一个TabHost时。只有文本可见,如果我将文本留空,则可以看到图标。我想在屏幕上看到两者 有人能给我一些建议吗 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Usuario usu = new Usuario(); usu.nombre = "fsdf"; li
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Usuario usu = new Usuario();
usu.nombre = "fsdf";
lista.add(usu);
adaptador = new AdaptadorCelda(this,lista);
ListView lstView = (ListView)findViewById(R.id.lista);
lstView.setAdapter(adaptador);
Button btn = (Button)findViewById(R.id.btnSalva);
btn.setOnClickListener(onSave);
Resources res = getResources();
TabHost tabs=(TabHost)findViewById(R.id.tabhost);
tabs.setup();
TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
spec.setContent(R.id.tab1);
spec.setIndicator("",res.getDrawable(android.R.drawable.ic_menu_rotate));
tabs.addTab(spec);
spec=tabs.newTabSpec("mitab2");
spec.setContent(R.id.tab2);
spec.setIndicator("ddd",
res.getDrawable(android.R.drawable.presence_invisible));
tabs.addTab(spec);
tabs.setCurrentTab(0);
spec.setIndicator("",res.getDrawable(android.R.drawable.ic_menu_rotate)) In this case icon appears.
spec.setIndicator("ddd",
res.getDrawable(android.R.drawable.presence_invisible));
下面是main.xml
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+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="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/lblNombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="17dp"
android:text="Nombre"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/txtNombre"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/lblNombre"
android:layout_marginLeft="35dp"
android:layout_toRightOf="@+id/lblNombre"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txtApellido"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtNombre"
android:layout_below="@+id/txtNombre"
android:ems="10" />
<TextView
android:id="@+id/lblApellido"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txtApellido"
android:layout_alignParentLeft="true"
android:text="Apellidos"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/tipos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/txtApellido"
android:layout_marginTop="35dp"
android:layout_toLeftOf="@+id/txtApellido" >
<RadioButton
android:id="@+id/rdbAlta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="alta" />
<RadioButton
android:id="@+id/rdbBaja"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="baja" />
<RadioButton
android:id="@+id/rdbTramite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tramite" />
</RadioGroup>
<Button
android:id="@+id/btnSalva"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txtApellido"
android:layout_alignTop="@+id/tipos"
android:layout_marginLeft="58dp"
android:layout_marginTop="30dp"
android:text="Button" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/txtApellido"
android:src="@drawable/ic_menu_chart" />
</RelativeLayout>
<LinearLayout
android:id="@+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lista"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tipos"
android:layout_marginTop="24dp" >
</ListView>
</LinearLayout>
</FrameLayout>
您是否在运行冰淇淋三明治的设备上测试了应用程序?我最近注意到,根据目标设备和android平台版本的不同,默认的
TabHost
表现不同
运行一个向设置指示器提供图标和文本的应用程序(如问题来源所示),测试结果如下:
- Android 2.1手机:显示图标和文字(图标下方的标签与预期一致)
- 带有4.0.3的手机:图标不显示,只有文本可见
- 运行ICS的平板电脑:显示图标和文本的选项卡
正如您所发现的,将标签替换为空字符串手机上出现了图标,但用户需要猜测图标的含义。除此之外:在pre-ICS手机上运行此版本的应用程序时:标签保持大小,并在图像下方留下空白区域
为了改变这个由设备驱动的关于选项卡应获得多少空间的决定,我在以下方面找到了解决方案:
首先,您需要提供自己的选项卡指示器布局(“教程中的layout/tab_indicator.xml”),包括图像视图
和文本视图
。然后通过setIndicator
告诉TabSpec
使用此选项。瞧:你也可以用ICS在手机上看到图标和标签
以下是关于如何设置指标的重要部分(这=当前活动):
如果您喜欢旧款和新款手机标签的相同外观,请在此处添加其他外观:
. 此页面生成图像和样式以自定义操作栏(包括选项卡),并帮助我了解如何定义9-patch背景图像。我尝试了以下方法,效果良好:
使用setIndicator(“选项卡”)仅设置文本
使用setBackgroundDrawable(-)设置图标
示例代码:
final TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
final Resources res = getResources();
int i;
tabHost.setup(); //Call setup() before adding tabs if loading TabHost using findViewById().
TabHost.TabSpec ts = tabHost.newTabSpec("trackinfo");
//ts.setIndicator("", res.getDrawable(R.drawable.icon_trackinfo));
ts.setIndicator("Track Information");
ts.setContent(R.id.tabTrackInfo);
tabHost.addTab(ts);
TabHost.TabSpec ts2 = tabHost.newTabSpec("collection");
//ts2.setIndicator("", res.getDrawable(R.drawable.icon_collection_gray));
ts2.setIndicator("My Collection");
ts2.setContent(R.id.tabMyCollecton);
tabHost.addTab(ts2);
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId)
{
if("trackinfo".equals(tabId)) {
//
TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(0);
TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextColor(TabColor[0]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_selected));
tabView = tw.getChildTabViewAt(1);
tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextColor(TabColor[1]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_gray));
}
if("collection".equals(tabId)) {
//
TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
View tabView = tw.getChildTabViewAt(0);
TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextColor(TabColor[1]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_gray));
tabView = tw.getChildTabViewAt(1);
tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setTextColor(TabColor[0]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_selected));
}
}}); // END OF tabHost.setOnTabChangedListener
// After Tabs being added
// change font settings
TabWidget tw = (TabWidget)tabHost.findViewById(android.R.id.tabs);
for(i=0;i<2;i++)
{
View tabView = tw.getChildTabViewAt(i);
TextView tv = (TextView)tabView.findViewById(android.R.id.title);
tv.setHeight(25);
tv.setTextColor(TabColor[i]);
tv.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD_ITALIC);
tv.setTextSize(20);
tabView.setBackgroundDrawable(res.getDrawable(TabIcon[i]));
}
final TabHost TabHost=(TabHost)findviewbyd(android.R.id.TabHost);
最终资源res=getResources();
int i;
tabHost.setup()//如果使用findViewById()加载TabHost,请在添加选项卡之前调用setup()。
TabHost.TabSpec ts=TabHost.newTabSpec(“trackinfo”);
//ts.setIndicator(“,res.getDrawable(R.drawable.icon_trackinfo));
ts.setIndicator(“轨道信息”);
ts.setContent(R.id.tabTrackInfo);
tabHost.addTab(ts);
TabHost.TabSpec ts2=TabHost.newTabSpec(“集合”);
//ts2.setIndicator(“,res.getDrawable(R.drawable.icon\u collection\u gray));
ts2.setIndicator(“我的收藏”);
ts2.setContent(R.id.tabMyCollecton);
tabHost.addTab(ts2);
tabHost.setOnTabChangedListener(新的ontabchangedListener(){
@凌驾
已更改的公共无效项(字符串选项卡ID)
{
if(“trackinfo”.equals(tabId)){
//
TabWidget tw=(TabWidget)tabHost.findviewbyd(android.R.id.tabs);
视图选项卡视图=tw.getChildTabViewAt(0);
TextView tv=(TextView)tabView.findviewbyd(android.R.id.title);
tv.setTextColor(TabColor[0]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_选中));
tabView=tw.getChildTabViewAt(1);
tv=(TextView)tabView.findviewbyd(android.R.id.title);
tv.setTextColor(TabColor[1]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_-gray));
}
if(“collection”.equals(tabId)){
//
TabWidget tw=(TabWidget)tabHost.findviewbyd(android.R.id.tabs);
视图选项卡视图=tw.getChildTabViewAt(0);
TextView tv=(TextView)tabView.findviewbyd(android.R.id.title);
tv.setTextColor(TabColor[1]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_-gray));
tabView=tw.getChildTabViewAt(1);
tv=(TextView)tabView.findviewbyd(android.R.id.title);
tv.setTextColor(TabColor[0]);
tabView.setBackgroundDrawable(res.getDrawable(R.drawable.icon_选中));
}
}}); // tabHost.setOnTabChangedListener的结尾
//添加选项卡后
//更改字体设置
TabWidget tw=(TabWidget)tabHost.findviewbyd(android.R.id.tabs);
对于(i=0;i有另一个非常简单的解决方案
问题似乎出现在新的Holo-theme和TabWidget
中。因此,您应该在values-v11
文件夹中定义Holo-theme,但使用旧的TabWidget
样式,如下所示:
@android:style/Widget.TabWidget
这对我很有用,希望能对别人有所帮助。这是一种在ICS中显示文本和图标的简单方法。设置Tabhost后,我调用以下方法:
setTabIcon(mTabHost, 0, R.drawable.ic_tab1); //for Tab 1
setTabIcon(mTabHost, 1, R.drawable.ic_tab2); //for Tab 2
public void setTabIcon(TabHost tabHost, int tabIndex, int iconResource) {
ImageView tabImageView = (ImageView) tabHost.getTabWidget().getChildTabViewAt(tabIndex).findViewById(android.R.id.icon);
tabImageView.setVisibility(View.VISIBLE);
tabImageView.setImageResource(iconResource);
}
如果您想要更高级的解决方案,您需要创建一个类似于@sunadoler推荐的布局。我的建议是,您可以使用holo主题布局创建布局,在sdk\u dir/platforms/android-14/data/res/layout/tab\u indicator\u holo.xml上搜索该文件
我的高级解决方案是下一个,您需要创建如下布局:
tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingBottom="5dp"
style="@android:style/Widget.Holo.Tab">
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="visible" />
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
style="@android:style/Widget.Holo.ActionBar.TabText" />
</LinearLayout>
最后,当您在活动或片段中创建选项卡时,请使用以下代码:
mTabHost.addTab(
mTabHost.newTabSpec("tab1")
.setIndicator(createTabIndicator(inflater, mTabHost, R.string.tab1, R.drawable.ic_tab1))
.setContent(R.id.tab1)
);
我已经在ICS中测试过这个解决方案,效果很好
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="5dp"
android:paddingBottom="5dp"
style="@android:style/Widget.Holo.Tab">
<ImageView
android:id="@android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="visible" />
<TextView
android:id="@android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
style="@android:style/Widget.Holo.ActionBar.TabText" />
</LinearLayout>
public View createTabIndicator(LayoutInflater inflater, TabHost tabHost, int textResource, int iconResource) {
View tabIndicator = inflater.inflate(R.layout.tab_indicator, tabHost.getTabWidget(), false);
((TextView) tabIndicator.findViewById(android.R.id.title)).setText(textResource);
((ImageView) tabIndicator.findViewById(android.R.id.icon)).setImageResource(iconResource);
return tabIndicator;
}
mTabHost.addTab(
mTabHost.newTabSpec("tab1")
.setIndicator(createTabIndicator(inflater, mTabHost, R.string.tab1, R.drawable.ic_tab1))
.setContent(R.id.tab1)
);