0

无法访问 tabhost 内的视图。我想访问 myTitle 布局以及 myTitleText 并以编程方式更改它们的属性。

尝试使用 findviewbyid 和 view.findviewbyid 但为空。尝试过充气机,但属性保持不变。

请帮忙

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

    <LinearLayout
        android:id="@+id/layout_myTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">

        <TextView
            android:id="@+id/title_myTitleText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TITLE" />
    </LinearLayout>

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

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="0dip" android:layout_weight="1.0"/>
</LinearLayout>

</TabHost>
4

2 回答 2

0

对于那些正在寻找有关如何访问 TabHost 中的视图的答案的人,请执行以下操作。

让你有一个像这样的 XML:

<LinearLayout ...

    <TabHost
        android:id="@+id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
            </TabWidget>

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <!-- Text 1 -->
                    <TextView 
                        android:id="@+id/text1"
                        android:text="Some text 1"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <!-- Text 2 -->
                    <TextView 
                        android:id="@+id/text2"
                        android:text="Some text 2"/>
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</LinearLayout>

如果你想访问text2,你应该首先访问一个容器(tab2)。

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
    public void onTabChanged(String tabId) {
        Log.i(TAG, "tabId = " + tabId);

        if (tabId.equals("tab2")) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.tab2);
            TextView text = (TextView) layout.findViewById(R.id.text2);
        }
    }
});
于 2016-09-06T15:43:51.173 回答
-1

使用 tabHost 完成

TextView tv = (TextView) tabHost.findViewById(R.id.title_myTitleText);
于 2013-04-03T11:05:57.083 回答