0

私はここでこの質問を見ました。解決策は、アイコンの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>

折れ線グラフと計算機はドローアブルフォルダにあります。私が逃したものはありますか?タブは正常に機能していますが、アイコンがありません...?


さて、私はこのようにそれをしました:

@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>
4

2 に答える 2

1

ドローアブルを取得していますが、設定していません。この方法を試してください:

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))
于 2012-11-13T17:24:33.473 に答える
0

これ

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

何もしません。取得したDrawableをどこにも設定していません。

于 2012-11-13T17:24:29.773 に答える