xiaoyan_12 2013-02-20 07:08 采纳率: 25%
浏览 6322
已采纳

标签图标为什么不能显示?

我想做一个简单的标签应用,这个标签应用中有两个选项卡。现在的问题是当我在标签应用中加入下面的代码,只显示文本,没有图标。如果我给文本加 "",图标就会显示出来。我用的android版本是4.0.3。
如何修改代码,让图标显示出来呢?

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+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" >

     <TabWidget android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs" />

     <FrameLayout android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent" >

        <LinearLayout android:id="@+id/tab1"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <TextView android:id="@+id/textView1"
                android:text="Contenido Tab 1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>

        <LinearLayout android:id="@+id/tab2"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <TextView android:id="@+id/textView2"
                android:text="Contenido Tab 2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>

     </FrameLayout>
</LinearLayout>
</TabHost>

activity 代码:

public class TabTestActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Resources res = getResources();

    TabHost tabs=(TabHost)findViewById(R.id.tabhost);
    tabs.setup();

    TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
    spec.setContent(R.id.tab1);
    spec.setIndicator("sss",
            res.getDrawable(android.R.drawable.ic_btn_speak_now));
    tabs.addTab(spec);

    spec=tabs.newTabSpec("mitab2");
    spec.setContent(R.id.tab2);
    spec.setIndicator("TAB2",
            res.getDrawable(android.R.drawable.ic_dialog_map));
    tabs.addTab(spec);
    tabs.setCurrentTab(0);
}

当我添加spec.setIndicator("", res.getDrawable(android.R.drawable.ic_dialog_map));,可以看见图标,但是当我添加spec.setIndicator("TAB2", res.getDrawable(android.R.drawable.ic_dialog_map));,我只能看见 TAB2 ,不能看见另一个,看似没有足够的空间来显示这两个选项卡,所以我又添加
tabs.getTabWidget().getChildAt(1).getLayoutParams().height = 150; 来增加选项卡的高度,还是没有管用。

  • 写回答

3条回答 默认 最新

  • 问答小助手 2013-02-21 02:32
    关注

    你只加载第一个,所以只能看见最新添加的那个。

    TabHost.TabSpec spec=tabs.newTabSpec("mitab1");
    
            spec.setIndicator("sss",
                    res.getDrawable(android.R.drawable.ic_btn_speak_now));
     Intent sssIntent = new Intent(this, First.class);
     spec.setContent(sssIntent);
            tabs.addTab(spec);
    
    TabHost.TabSpec spec2=tabs.newTabSpec("mitab2");
            spec2=tabs.newTabSpec("mitab2");
            spec2.setIndicator("TAB2",
                    res.getDrawable(android.R.drawable.ic_dialog_map));
    Intent sssIntent2 = new Intent(this, Second.class);
     spec2.setContent(sssIntent2 );
            tabs.addTab(spec2);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式