Frage

I saw this question here. The solution was to make a xml for a icon and then getResources().getDrawable(R.drawable.tabicon); on in. So this is in my java:

TabHost tabhost = (TabHost)findViewById(R.id.tabhost);
    tabhost.setup();
    TabSpec tabspecs1 = th.newTabSpec("example");
    tabspecs1.setContent(R.id.tab1);
    tabspecs1.setIndicator("Example");
    getResources().getDrawable(R.drawable.tabicon);
    th.addTab(tabspecs1);
    listView = getListView();

My tabicon.xml:

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

line chart and calculator are in drawable-folders. Anything I missed? Tabs are working fine, but no icons...?


Okay I did it like this:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    setTitle("MyApp");
    listView = getListView();
    TabHost th = (TabHost)findViewById(R.id.tabhost);
    th.setup();
    TabSpec tabspecs1 = th.newTabSpec("tag01");
    tabspecs1.setContent(R.id.tab1);
    tabspecs1.setIndicator ("exlample"),getResources().getDrawable(R.drawable.tabicon));
    th.addTab(tabspecs1);
    TabSpec tabspecs2 = th.newTabSpec("tag02");
    tabspecs2.setContent(R.id.tab2);
    tabspecs2.setIndicator ("lululu", getResources().getDrawable(R.drawable.tabicon));
    th.addTab(tabspecs2);
}

I used tabicon.xml for both, just 4 testing.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use icon1 -->
<item android:drawable="@drawable/ic_icon1"
      android:state_selected="true" />
<!-- When not selected, use icon2-->
<item android:drawable="@drawable/ic_icon2" />
</selector>
War es hilfreich?

Lösung

You're getting the drawable, but not setting it. Try this method:

http://developer.android.com/reference/android/widget/TabHost.TabSpec.html#setIndicator(java.lang.CharSequence, android.graphics.drawable.Drawable)

setIndicator ("Example", getResources().getDrawable(R.drawable.tabicon))

Andere Tipps

This

getResources().getDrawable(R.drawable.tabicon);

doesn't do anything. You're not setting the retrieved Drawable anywhere.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top