选项卡图标未显示

8
我正在尝试在Android上创建一个简单的带有两个选项卡的应用程序。我的问题是,当我将这段代码放在选项卡中时,只显示文本,但不显示图标。如果我将文本设置为“”,则图标会显示出来。
请问是否有人能够帮助我?我的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>

活动代码是

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; 

但似乎没有起作用。

4个回答

8

我将标签名称替换为空值。 现在我可以看到仅有的图标了。 没有找到其他解决方案。

TabHost.TabSpec spec=tabs.newTabSpec("mitab1");

spec.setIndicator("",
                  res.getDrawable(android.R.drawable.ic_btn_speak_now));
Intent sssIntent = new Intent(this, First.class);
spec.setContent(sssIntent);
tabs.addTab(spec);

你是对的..我们必须清除选项卡的标签才能看到图标..真是糟糕... 顺便感谢你的帮助,伙计。 - Noman
如果place为null,我能够看到图标,但是下面的文本我无法看到...可以告诉我如何同时显示图标和文本... - vinay Maneti

4

//您正在超载第一个,因此只能看到最后添加的一个

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

你能再解释一下吗?当你说重载时,我不太明白。非常感谢。 - theholy

2
图标(连同标签)在选项卡中的可见性取决于目标设备和Android平台版本。
我深入研究了这个问题,并在您的其他(非常相似)关于此问题的问题中添加了更多细节和解决方案;它可以在这里找到:https://dev59.com/EGgv5IYBdhLWcg3wPOfz#11379708

1
在AndroidManifest.xml中添加这个解决了问题。
<application 
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
</application>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接