【问题标题】:Centering Tab Names On Android在 Android 上居中选项卡名称
【发布时间】:2012-03-15 04:49:53
【问题描述】:

我遵循了官方的 HelloTabWidget Android 教程,它运行良好。接下来我删除了选项卡图标,它仍然可以正常工作。现在我想在每个选项卡中将 tabtext 水平和垂直居中,但我不知道如何。我试过搜索,但找不到解决方案。

我尝试将 android:layout_gravity 添加到我的 main.xml 文件中,但没有帮助。

这是我的 onCreate main.java 文件:

公共无效 onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main); TabHost tabHost = getTabHost(); // 活动 TabHost TabHost.TabSpec 规范; // 每个选项卡的可重用 TabSpec 意图意图; // 每个选项卡的可重用意图 intent = new Intent().setClass(this, Indkobsliste.class); 规格 = tabHost.newTabSpec("tab1").setIndicator("Tab1") .setContent(意图); tabHost.addTab(spec); intent = new Intent().setClass(this, Opskrifter.class); 规格 = tabHost.newTabSpec("tab2").setIndicator("Tab2") .setContent(意图); tabHost.addTab(spec); intent = new Intent().setClass(this, Madplan.class); 规格 = tabHost.newTabSpec("tab3").setIndicator("Tab3") .setContent(意图); tabHost.addTab(spec); tabHost.setCurrentTab(1); }

这是 main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="5dp"/>
    </LinearLayout>
</TabHost>

这是我的标签现在的样子:

【问题讨论】:

    标签: android layout tabwidget


    【解决方案1】:

    如果您运行的是 Android API Level >= 4,则可以使用TabHost.TabSpec.setIndicator(View view)。这允许您指定任何视图,因此只需提供一个位于您vertically center your text 的视图。

    但是,如果您运行的 API 级别较低,请尝试使用来自 this other stackoverflow question 的答案。它建议使用反射来设置视图,即使 API 没有发布所需的 setIndicator 方法。

    【讨论】:

    • 非常感谢。 .setIndicator 确实抱怨我使用 int,所以我在 Stackoverflow 上进行了搜索,我看到了这个:link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2010-10-09
    • 1970-01-01
    • 2022-08-05
    • 1970-01-01
    相关资源
    最近更新 更多