tabhost /tabbar icons not showing

2019-08-01 11:55发布

my tab bar icon wont show up

TabHost tabHost = getTabHost();

TabSpec barcodeInsertSpec = tabHost.newTabSpec("Barcode Insert");
barcodeInsertSpec.setIndicator("Barcode Insert", getResources().getDrawable(R.drawable.home2));
barcodeInsertSpec.setContent(new Intent(getBaseContext(), BarcodeInsertActivity.class));

tabHost.addTab(barcodeInsertSpec);

drawable/home2.xml

<?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- When not selected, use that-->
        <item android:drawable="@drawable/home22" />
    </selector>

and put picture in three folders in different sizes (48x48, 32x32, 24x24) drawable-hdpi , drawable-ldpi, ... as .png

like drawable-hdpi/home22.png

5条回答
地球回转人心会变
2楼-- · 2019-08-01 12:00

Try This.

  TabHost th = getTabHost();
  TabHost.TabSpec sp;
  sp=th.newTabSpec("tab 1");
  sp.setIndicator("Home");
  sp.setContent(new Intent(this, Welcome.class));
  th.addTab(sp);
  tabhost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.YOURIMAGE);

*You can also use a custom item selector in place of drawable to make a focused and pressed effects. Cheers!

查看更多
爷的心禁止访问
3楼-- · 2019-08-01 12:17

I had the same problem using Xamarin.

This was a hack solution that shows the icon but NOT the text (looks like an android defect):

This solution shows both, but it hosed my app-wide styles, which indicates to me that I could probably set up a style override:

EDIT 10 OCT 2013 - I got it to work in c#! It should be straightforward to do the same in java

I was able to get tabs to show up with icons with code and a custom style. See below

tab_indicator_holo.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="@dimen/tab_host_default_height"
android:orientation="horizontal"
style="@style/TabBlood"
android:gravity="center">

<ImageView
    android:id="@android:id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:visibility="visible"
    android:contentDescription="@null" />

<TextView
    android:id="@android:id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    style="@style/TabTextBlood" />

</LinearLayout>

My Activity OnCreate:

...
var spec = this.TabHost.NewTabSpec(tag);
var drawableIcon = Resources.GetDrawable(drawableId);
View tabIndicator = LayoutInflater.From(this).Inflate(Resource.Layout.tab_indicator_holo, this.TabHost.TabWidget, false);
TextView title = tabIndicator.FindViewById<TextView>(Android.Resource.Id.Title);
ImageView img = tabIndicator.FindViewById<ImageView>(Android.Resource.Id.Icon);
title.Text = label;
img.SetImageDrawable(drawableIcon);
spec.SetIndicator(tabIndicator);
spec.SetContent(intent);
this.TabHost.AddTab(spec);
查看更多
够拽才男人
4楼-- · 2019-08-01 12:23

This code is work for me :)

Resources res=getResources();
TabHost Tabs = (TabHost) findViewById(R.id.your_id);
Tabs.setup();
TabHost.TabSpec spec;
spec = Tabs.newTabSpec("tag1");
spec.setContent(R.id.tab1);
spec.setIndicator("Home", res.getDrawable(R.drawable.home_icon));
Tabs.addTab(spec);
查看更多
Luminary・发光体
5楼-- · 2019-08-01 12:25

try this code

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
     <item  android:state_focused="true"
            android:state_pressed="true"
            android:drawable="@drawable/button0_click"></item>
    <item   android:state_focused="true"
            android:state_pressed="false"
            android:drawable="@drawable/button0_click"></item>
    <item
            android:state_focused="false"
            android:state_pressed="true"
            android:drawable="@drawable/button0_click" />
     <item 
            android:drawable="@drawable/button0"></item>

</selector>
查看更多
ゆ 、 Hurt°
6楼-- · 2019-08-01 12:26

Hi Use the drawable selector file like this.

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
  <item
    android:state_selected="true"
    android:drawable="@drawable/select" />
  <item
    android:state_selected="false"
    android:drawable="@drawable/deselct" />
</selector>
查看更多
登录 后发表回答