Android TextView 文本未显示在选项卡内?

发布于 2025-01-03 04:42:06 字数 2368 浏览 3 评论 0原文

请原谅我的简单问题,因为我是 Android 新手。 :)

为什么文本视图的文本属性没有显示在此选项卡内?

屏幕: 这里

这是我的代码..

MapTab2.java

package com.test.maptab2;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class MapTab2 extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost.TabSpec spec;

        spec = getTabHost().newTabSpec("tab1");
        spec.setContent(R.id.mapstub);
        spec.setIndicator("Map");
        getTabHost().addTab(spec);

        spec = getTabHost().newTabSpec("tab2");
        spec.setContent(R.id.detailstub);
        spec.setIndicator("Detail");
        getTabHost().addTab(spec);

        getTabHost().setCurrentTab(0);

    }

}

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="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- Tab-switch panel -->
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <!-- Tab contents -->
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <!-- Map here -->
            <TextView android:id="@+id/mapstub"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="map"/>

            <!-- Other stuff -->
            <TextView android:id="@+id/detailstub"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="detail"/>

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

I我正在尝试构建在选项卡中显示地图的方法。我认为最好先了解基础知识。 :')

Forgive my simple questions, as I'm new to android. :)

Why is the text property of the textview not displaying inside this tab?

Screen: Here

Here's my code..

MapTab2.java

package com.test.maptab2;

import android.app.TabActivity;
import android.os.Bundle;
import android.widget.TabHost;

public class MapTab2 extends TabActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TabHost.TabSpec spec;

        spec = getTabHost().newTabSpec("tab1");
        spec.setContent(R.id.mapstub);
        spec.setIndicator("Map");
        getTabHost().addTab(spec);

        spec = getTabHost().newTabSpec("tab2");
        spec.setContent(R.id.detailstub);
        spec.setIndicator("Detail");
        getTabHost().addTab(spec);

        getTabHost().setCurrentTab(0);

    }

}

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="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- Tab-switch panel -->
        <TabWidget android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>

        <!-- Tab contents -->
        <FrameLayout android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <!-- Map here -->
            <TextView android:id="@+id/mapstub"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="map"/>

            <!-- Other stuff -->
            <TextView android:id="@+id/detailstub"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="detail"/>

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

I'm trying to build my way up to displaying a map within a tab. Best to understand the basics first, I thought. :')

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

倒数 2025-01-10 04:42:06

实际上,我更幸运的是在父视图(在本例中为 LinearLayout)中设置重力,然后在子视图(在本例中为 TextViews 等)中设置 layout_gravity 属性。

为了让这个简单的示例正常工作,我会将所有“fill_parent”属性更改为“wrap_content”(或 0.0dp 作为另一种选择) - 然后您可以开始使用不同的属性,以使所有内容都按照您想要的方式准确定位。

I actually have had more luck setting the gravity in the parent view (in this case, your LinearLayout), then setting the layout_gravity attributes in the child views (in your case, your TextViews, etc).

To get this simple example working, I would change all your "fill_parent" attributes to "wrap_content" (or 0.0dp as another alternative) - then you can start playing with the different attributes to get everything positioned exactly how you would like.

秋心╮凉 2025-01-10 04:42:06

尝试在选项卡内的每个 TextView 中添加 android:gravity="center" 。

Try adding android:gravity="center" in every TextView inside a Tab.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文