图标未显示在标签主机中

时间:2012-07-06 06:24:07

标签: android image android-tabhost

在我的应用程序中使用Tab主机活动并设置带有图片的选项卡。当应用程序在模拟器上运行时,图像显示没有任何问题。但是,当我在移动设备上运行应用程序(Micromax mobile)时,它不会显示。

请告诉我问题是什么以及如何解决。

标签主机编码:

        Intent intent = new Intent(this, WeekActivity.class);

    tabHost.addTab(tabHost
            .newTabSpec("Week")
            .setIndicator("Weekly", res.getDrawable(R.drawable.ic_tab_main))
            .setContent(intent));

    Intent intent2 = new Intent(this,ListMonthActivity.class);
    tabHost.addTab(tabHost
            .newTabSpec("Month")
            .setIndicator("Monthly", res.getDrawable(R.drawable.ic_tab_setup))
            .setContent(intent2));
    tabHost.setCurrentTab(0);

    Intent intent3 = new Intent(this, ListYearActivity.class);
    TabHost.TabSpec spec=tabHost.newTabSpec("Year").setIndicator("Yearly",getResources().getDrawable(R.drawable.ic_launcher)).setContent(intent3);
    tabHost.addTab(spec);

    /*tabHost.addTab(tabHost
            .newTabSpec("Year")
            .setIndicator("Yearly", res.getDrawable(R.drawable.ic_tab_setup))
            .setContent(intent3));
    tabHost.setCurrentTab(0);*/

    // Set tabs Colors
    tabHost.setBackgroundColor(Color.BLACK);
    tabHost.getTabWidget().setBackgroundColor(Color.BLACK);
   }

选择器选项卡主机Xml文件

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

Tabhost布局XML文件:

   <?xml version="1.0" encoding="utf-8"?>
   <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:padding="0dp"
    >
    <TabWidget android:id="@android:id/tabs"
        android:layout_width="fill_parent" android:layout_height="wrap_content"
        />
    <FrameLayout android:id="@android:id/tabcontent"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        />

</LinearLayout>
   </TabHost>

3 个答案:

答案 0 :(得分:2)

您必须将图像保存在每个文件夹中,例如mdpi,hdpi,ldpi,并且您必须将图片保存在mdpi文件夹中,低分辨率或高密度移动设备会自动从mdpi foleder中选择图像。

答案 1 :(得分:2)

当我给出下面给出的代码时,它成功地运行了。

mTabHost.addTab(mTabHost.newTabSpec("Bar").setIndicator("",getResources().getDrawable(R.drawable.ic_barchart)), BarChart.class, null);

我所做的更改只是将setIndicator的第一个参数保留为EMPTY字符串。 (当我将其作为"BarChart"给出时,它不起作用。)

答案 2 :(得分:1)

将标签主机中的图像设置为:

TabHost.TabSpec spec=tabHost.newTabSpec("hello").setIndicator("hello",getResources().getDrawable(R.drawable.ic_launcher)).setContent(intent);
tabHost.addTab(spec);

我刚刚在设备中尝试了这个并且工作正常。

相关问题