Tab中的图标(API 16+)

时间:2012-11-13 17:22:41

标签: android drawable tabwidget

我看到了这个问题here。解决方案是为图标制作xml然后getResources().getDrawable(R.drawable.tabicon); 在。所以这是在我的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();

我的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>

折线图和计算器位于drawable文件夹中。 我错过了什么?标签工作正常,但没有图标......?


好的,我这样做了:

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

我使用了tabicon.xml,只进行了4次测试。

<?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>

2 个答案:

答案 0 :(得分:1)

你得到了drawable,但没有设置它。试试这个方法:

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))

答案 1 :(得分:0)

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

什么都不做。您没有在任何地方设置检索到的Drawable。

相关问题