【问题标题】:Android TabHost content not showingAndroid TabHost 内容未显示
【发布时间】:2013-03-07 01:30:48
【问题描述】:

我创建了一个 TabHost 并分配了 4 个带有选项卡的活动意图,这些似乎工作得很好。我唯一的问题是活动内容未显示在我的 tabhost 视图的框架布局 #tabcontent 中。

我已按照官方参考并扫描互联网寻找解决方案,但我看不出问题出在哪里。

Log.v("Activity", "Reports") 记录在 ant 中,因此它确实执行了活动。因此,我猜测是我的 ReportsActivity 中的 setContentView() 导致了问题。但我是新手,所以我真的说不出来。 (没有生成错误)

这是我的标签主机

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@android:id/tabhost"
    android:background="#FFFFFF">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#FFFFFF">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5sp"
            android:layout_weight="1" />

    </LinearLayout>

</TabHost>

这就是我在 TabActivity 中添加标签的方式

// Glob
Intent intent;
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Resources res = getResources();

// Reports tab
intent = new Intent().setClass(this, ReportsActivity.class);
spec = tabHost.newTabSpec("reports")
        .setIndicator(
                res.getText(R.string.reports),
                res.getDrawable(R.drawable.reports))
        .setContent(intent);
tabHost.addTab(spec);

这是我的内容活动(R.layout.reports = 带有文本视图的线性布局)

public class ReportsActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reports);

        Log.v("Activity", "Reports");
    }
}

【问题讨论】:

    标签: android android-tabhost android-tabactivity


    【解决方案1】:

    尝试像这样实现您的 TabSpec:

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    
    spec = tabHost.newTabSpec("reports")
                  .setIndicator(
                      res.getText(R.string.reports),
                      res.getDrawable(R.drawable.reports))
                  .setContent(intent);
    

    【讨论】:

    • TabSpec 没有方法 addFlags 所以我尝试在我的意图上使用标志,但 tabcontent 仍然没有显示我的 ReportsActivity 线性布局。 编辑:我的 ReportsActivity 是否应该扩展默认 Activity 超类以外的任何其他活动?
    • 如果我在 xml 中删除 TabWidget,FrameLayout 会突然显示活动内容。我尝试使用边距来查看 TabWidget 是否与 FrameLayout 重叠,但事实并非如此(我认为)。 TabWidget 应该与此有关吗?
    • 我没有看到,您在 tabspec 之外初始化了意图。 addFlag 属于意图。请参阅我的更正答案。
    • 是的,我明白了 - 问题仍然存在。如果我从我的 XML 中删除 TabWidget 元素,框架布局将显示活动内容,但如果我再次实现 tabwidget 元素,活动内容将不会显示。所以,我猜我的 setContentView 或 tabhost 有问题?
    • 原来tabhost中的linearlayout需要android:orientation="vertical",现在一切正常。
    【解决方案2】:

    这是因为您选择了默认为水平布局的线性布局。 如果您在 LinearLayout 标记内设置 android:orientation="vertical" 应该可以解决您的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-03
      • 2013-06-25
      • 2017-02-13
      • 1970-01-01
      • 1970-01-01
      • 2013-02-11
      • 2016-11-14
      • 1970-01-01
      相关资源
      最近更新 更多