【问题标题】:TabHost - how to change tab text in XMLTabHost - 如何更改 XML 中的选项卡文本
【发布时间】:2013-06-13 13:11:19
【问题描述】:

我知道如何以编程方式更改它的解决方案,但是我想在 XML 中设置文本。你是怎样做的?我看过这里:http://developer.android.com/reference/android/widget/TabHost.html,但没有找到解决方案。

【问题讨论】:

  • 真的有用吗?我遇到了和其他人一样的问题。您是否需要为它编写额外的 java 代码?

标签: android android-tabhost


【解决方案1】:

怎么样……

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TabHost 
        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">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:tag="tab0"
                    android:text="Tab 1"
                    android:background="@android:drawable/btn_star_big_on"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    />
                <TextView
                    android:tag="tab1"
                    android:text="Tab 2"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    />
                <TextView
                    android:tag="tab2"
                    android:text="Tab 3"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    />

            </TabWidget>

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

                <TextView
                    android:text="Hallo1"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
                <TextView
                    android:text="Hallo2"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />
                <TextView
                    android:text="Hallo3"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent" />

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

</RelativeLayout>

这样它会如下所示:

查看完整的标签示例here

希望这会有所帮助....干杯!

【讨论】:

  • @Supuhstar:您能解释一下您遇到错误的原因或错误吗?
  • 没有错误,只是......绝对没有任何效果。 TextViews 出现在预览中选项卡的左侧,在实际设备上运行时不显示任何选项卡。没有获得任何功能,并且片段彼此重叠
  • 取决于你如何使用上面的 XML;尝试以下操作:将 ID 添加到所有选项卡(即TextViews,例如android:id="@+id/tab1",...,android:id="@+id/content1",...)并从android:text 中删除android:text 标签以及未定义的符号。这会产生一个干净的 UI 预览......
  • 感谢您的提示!我最终以编程方式更改它们
  • 我正在尝试做同样的事情并得到与 Supuhstar 相同的结果。在 tabwidget 中添加 textview 只会在现有选项卡的左侧产生文本,看起来创建了 6 个选项卡,而不是默认的 3 个。所以我看到了您最近的评论,但是如果您从 tabwidget 中的文本视图中删除 android:text 标签,你回到没有标签的标签是吗?
【解决方案2】:

您始终可以进入@+id/tab1 并将其更改为您希望调用选项卡的任何内容。因此,如果您希望将其称为“关于”,只需在特定选项卡的线性布局中将其更改为 @+id/About。

【讨论】:

  • 问题是关于 XML 的。您给出的公式是 xpath 查询吗?如果有,请举个例子,可能大家看不懂xpath。
  • 这一点很晚,但 jorgensen 正在谈论标签 ID,默认情况下,TabHost 使用 ID 作为标签标签。但正如@gattsbr 指出的那样,该解决方案仅在您不使用字符串翻译文件时才可用
猜你喜欢
  • 2011-05-29
  • 1970-01-01
  • 2018-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多