java - 如何在 TabHost 中为两个不同的选项卡显示相同的布局?

标签 java android android-layout android-tabhost android-tabs

我正在开发一个 Android 应用程序,它的 Activity 使用选项卡布局。有两个选项卡可以在某些 TextView 中显示的内容之间切换。

这意味着两个选项卡规范指向相同的内容布局(线性),R.id.plantilla:

<?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:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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" >

        <include layout="@layout/plantilla"/>


    </FrameLayout>
</LinearLayout>

但这仅在我切换到选项卡 2 并返回到选项卡 1 时有效,即当 Activity 启动时,在更改选项卡之前看不到布局“plantilla”。这是我的问题。

解决这个问题的最简单方法是什么?

PD:我试图复制这条线

<include layout="@layout/plantilla">

在 tabhost xml 中,但在这种情况下,我无法使用 findViewById(R.id.someTextView); 从 Java 代码访问 TextViews 对象;

最佳答案

我不相信你可以使用 include,我认为你需要在 xml 中使用不同的 id 标签实际定义你的布局两次。

或者您可以简单地定义容器 View ,然后以编程方式向其中添加 View 。我以前做过。这是我的做法:

制表布局

<?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:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="5dp" >
            <TextView
                    android:id="@+id/setupheader"
                    android:layout_width="fill_parent"
                    android:layout_height="20dp"
                    android:textSize="15dp" />
            <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="30dp"
                    android:gravity="bottom" />
            <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:padding="5dp" >
                    <!-- General Info Tab -->
                    <LinearLayout
                            android:id="@+id/general_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                    </LinearLayout>
                    <!-- Tool Tab -->
                    <LinearLayout
                            android:id="@+id/tool_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                            <ListView
                                    android:id="@+id/list1"
                                    android:layout_width="match_parent"
                                    android:layout_height="0dp"
                                    android:layout_weight="1"
                                    android:drawSelectorOnTop="false" />
                    </LinearLayout>
                    <!-- Offset Tab -->
                    <LinearLayout
                            android:id="@+id/offset_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="wrap_content"
                            android:orientation="vertical" >
                            <ListView
                                    android:id="@+id/list2"
                                    android:layout_width="match_parent"
                                    android:layout_height="0dp"
                                    android:layout_weight="1"
                                    android:drawSelectorOnTop="false" />
                    </LinearLayout>
                    <!-- Notes Tab -->
                    <LinearLayout
                            android:id="@+id/note_tab"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent"
                            android:orientation="vertical" >
                    </LinearLayout>
            </FrameLayout>
    </LinearLayout>
</TabHost>

以下是我的选项卡 Activity (我删除了重要部分,但它应该足够清楚地显示我是如何在没有每个选项卡单独 Activity 的情况下处理选项卡的)。

tabactivity

public class SetupDisplay extends TabActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.setupdetailmain);

            Bundle extras = getIntent().getExtras();
            RowId = extras.getLong("RowId");
            StartTab = extras.getInt("StartTab");

            // ***************************************************************
            // Set up the tabs in the tabhost
            // ***************************************************************
            tabHost = getTabHost();
            TabHost.TabSpec spec;
            spec = tabHost.newTabSpec("General").setIndicator("General")
                            .setContent(R.id.general_tab);
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Tools").setIndicator("Tools")
                            .setContent(R.id.tool_tab);
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Offsets").setIndicator("Offsets")
                            .setContent(R.id.offset_tab);
            tabHost.addTab(spec);
            spec = tabHost.newTabSpec("Notes").setIndicator("Notes")
                            .setContent(R.id.note_tab);
            tabHost.addTab(spec);
            populateTabs(StartTab);
    }

    // ***************************************************************
    // Clear views from linear layout tabs
    // ***************************************************************
    private void clearTabs() {
            general.removeAllViews();
            notes.removeAllViews();
    }

    // ***************************************************************
    // Fill the tabs
    // ***************************************************************
    private void populateTabs(int TabShown) {

            generaltab();
            tooltab();
            notestab();
            offsettab();

            tabHost.setCurrentTab(TabShown);
    }

    // ***************************************************************
    // Fill the General tab
    // ***************************************************************
    private void generaltab() {
        general = (LinearLayout) findViewById(R.id.general_tab);
        String prgdisp = SETUPINFO_PGM1;
        if (SETUPINFO_PGM2 != null) {
            prgdisp = prgdisp + ", " + SETUPINFO_PGM2;
        }
        if (SETUPINFO_PGM3 != null) {
            prgdisp = prgdisp + ", " + SETUPINFO_PGM3;
        }
        TextView programs = new TextView(this);
        programs.setText("Program(s): " + prgdisp);
        programs.setTextSize(20);
        general.addView(programs);

        if (SETUPINFO_KIT == null || SETUPINFO_KIT.equals("No Kit")) {
        } else {
            TextView kit = new TextView(this);
            kit.setText("Kit: " + SETUPINFO_KIT);
            kit.setTextSize(20);
            general.addView(kit);
        }

        if (SETUPINFO_FIXTURE1 == null && SETUPINFO_FIXTURE2 == null) {
        } else {
            String fixtdisp = SETUPINFO_FIXTURE1;
            if (SETUPINFO_FIXTURE2 != null) {
                fixtdisp = fixtdisp + ", " + SETUPINFO_FIXTURE2;
            }
            TextView fixtures = new TextView(this);
            fixtures.setText("Fixture(s): " + fixtdisp);
            fixtures.setTextSize(20);
            general.addView(fixtures);
            TextView fixtureloc = new TextView(this);
            fixtureloc.setText("Fixture Location: " + SETUPINFO_FIXTURELOC);
            fixtureloc.setTextSize(20);
            general.addView(fixtureloc);
        }

        if (SETUPINFO_VISE == null) {
        } else {
            TextView vise = new TextView(this);
            vise.setText("Vise(s): " + SETUPINFO_VISE);
            vise.setTextSize(20);
            general.addView(vise);
        }
    }

    // ***************************************************************
    // Fill the Offset tab
    // ***************************************************************
    private void offsettab() {
            wpccount = 0;
            for (int i = 0; i < 20; i++) {
                    if (wpcdesc[i] != null) {
                            wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i];
                            wpcidhold[wpccount] = wpcid[i];
                            wpcdeschold[wpccount] = wpcdesc[i];
                            wpccount++;
                    }
            }
            wpcdisplay = new String[wpccount];
            for (int i = 0; i < wpccount; i++) {
                    wpcdisplay[i] = wpcdisplayhold[i];
            }
            mWPCView = (ListView) findViewById(R.id.list2);
            mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this,
                            wpcdisplay, "Offset"));
            registerForContextMenu(mWPCView);
    }

    // ***************************************************************
    // Fill the Notes tab
    // ***************************************************************
    private void notestab() {
            notes = (LinearLayout) findViewById(R.id.note_tab);
            notestxt = new TextView(this);
            notestxt.setText(SETUPINFO_NOTES);
            notestxt.setTextSize(15);
            notes.addView(notestxt);
    }

}

希望这对您有所帮助。

关于java - 如何在 TabHost 中为两个不同的选项卡显示相同的布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9852534/

相关文章:

java - CouchbaseClient VS CouchbaseCluster

java - 为什么 BlueJ 给我的代码一个 "unreachable statement"错误?

android - 非空字段在 Glide 中为空

android - Gradle 错误 : package com. facebook 对于 android studio 不存在

java - 如何在 Intellij Idea 代码清理中自动删除不需要的换行符

java - 如何在 IE 中查找框架内的元素?

android - 如何替换旧 API<21 中的 NestedScrollView?

android - 在android中的类中的对话框 fragment 的线性布局中添加textview

android - 对话框中 EditText 的全息主题

Android RatingBar 在某些设备上显示黑色覆盖