【问题标题】:Are label and icon mutually exclusive in a TabHost TabSpec setIndicator?TabHost TabSpec setIndicator 中的标签和图标是否互斥?
【发布时间】:2013-09-13 01:11:44
【问题描述】:

在 Android 应用程序(最小 sdk 11,目标 sdk 18)上,扩展 Fragment 的类应该创建带有一个标签和一个图标的 TabHost 选项卡(TabSpec)。但是……

TabSpec ts1;
// if the label is set to "Home", the label is displayed but not the image
ts1 = tab_host.newTabSpec("TAB_ONE").setIndicator("Home", getActivity().getResources().getDrawable(R.drawable.ic_tabone)).setContent(R.id.edit_species_tab);

// if the label is null or empty, the image is displayed
ts1 = tab_host.newTabSpec("TAB_ONE").setIndicator(null, getActivity().getResources().getDrawable(R.drawable.ic_tabone)).setContent(R.id.edit_species_tab);

据我所知,documentation 没有提到标签和图标是互斥的。

起初我以为标签有一个纯色的背景色来隐藏图标,但事实并非如此。事实上,当我设置 TabHost 背景时,我可以看到标签是透明的:

tab_host.getTabWidget().setBackgroundResource(R.drawable.ic_mybackground);

如何设置 TabSpec 背景,以便为每个选项卡同时显示标签 图标?

tab_host.xml

<TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom" />

<FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingTop="65dp" >

    <LinearLayout
        android:id="@+id/edit_species_tab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/edit_regions_tab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/edit_quiz_tab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/edit_about_tab"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="5dp" >
    </LinearLayout>
</FrameLayout>

【问题讨论】:

    标签: android android-layout android-tabhost android-drawable fragment-tab-host


    【解决方案1】:

    holo主题中,tab只支持label或者icon,并且互斥。

    在 Holo 之后,默认情况下标签应该只有标签,请查看设计指南。 http://developer.android.com/design/building-blocks/tabs.html

    如果您愿意同时标记和图标,有两种选择。

    选项 1:使用旧的 Gingerbread 主题。设置你的 android:targetSdkVersion 9 或更低。 (蜂窝之前)

    选项 2:为您的标签使用自定义布局和视图。当然,这需要一些努力,但您可以使用任何您想要的自定义布局。例如:

    在您的活动代码中:

        TabHost.TabSpec ts1;
        View tabView = getLayoutInflater().inflate(R.layout.custom_tab, tab_host, false);
        ts1 = tab_host.newTabSpec("TAB_ONE").setIndicator(tabView).setContent(R.id.edit_species_tab);
    

    在你的布局中:(随便你)

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab"
    android:layout_width="80dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        />
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"
        />
    </LinearLayout>
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多