android - 标签图标不显示

标签 android tabs android-tabhost android-4.0-ice-cream-sandwich

我正在尝试在 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; 

但似乎不起作用。

最佳答案

我用空值替换了标签名称。 现在我可以单独看到图标了.. 找不到任何其他解决方案。

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

关于android - 标签图标不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963252/

相关文章:

android - 如果 View 计数较小,RecyclerView 不会回收 View

iOS Xamarin 将 tabBar 推到导航 Controller 上

ionic-framework - ionic 2/3 : Show and hide ion-tab/tab dynamically

Xcode 4.1行为-自动关闭标签页吗?

android - 收到错误消息 "Could not find com.google.gms:google-services:2.0.0-alpha6."

java - 如何从ListView项目中获取文本?

android - android 2.2 中的 TabHost 空指针异常

java - Android 示例中的 tabhost 在模拟器中强制关闭

android - 从搜索栏更改拇指颜色

Android - TabHost/TabWidget