簡體   English   中英

Android標簽無法正常工作

[英]Android Tabs not working correctly

我仍然是Android編程的新手,所以這可能是我很容易忽視的東西。

我正在構建針對Android 4及更高版本的應用,我已經實現了一個標簽主機。 (我在操作欄選項卡上閱讀了Android開發人員指南,但我仍然不明白。)無論如何,一切正常,除非我切換標簽 - 當我這樣做時,前一個標簽中的內容仍然重疊。 例如,我有標簽1,2,3和按鈕1,2和3.當我從按鈕1切換到3時,它們是重疊的。

fragment.java

public class fragment extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment);
        TabHost th = (TabHost)findViewById(R.id.tabhost);
        th.setup();
        TabSpec ts = th.newTabSpec("tag1");
        ts.setContent(R.id.tab1);
        ts.setIndicator("Tab one");
        th.addTab(ts);
        ts.setContent(R.id.tab2);
        ts.setIndicator("Tab two");
        th.addTab(ts);
        ts.setContent(R.id.tab3);
        ts.setIndicator("Tab Three ");
        th.addTab(ts);
    }
}

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        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" >

                <Button
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="NUmber one" />

            </LinearLayout>

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

                <Button
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Number two"/>
            </LinearLayout>

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

                <Button
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="NUmber three" />
            </LinearLayout>
        </FrameLayout>
    </LinearLayout>

</TabHost>

</LinearLayout>

任何幫助,將不勝感激。

問題是

你寫過TabSpec ts = th.newTabSpec("tag1"); 僅適用於Tab1。

並重復使用兩個標簽的其余部分。

從中更改您的代碼

    TabSpec ts = th.newTabSpec("tag1");
    ts.setContent(R.id.tab1);
    ts.setIndicator("Tab one");
    th.addTab(ts);
    ts.setContent(R.id.tab2);
    ts.setIndicator("Tab two");
    th.addTab(ts);
    ts.setContent(R.id.tab3);
    ts.setIndicator("Tab Three ");
    th.addTab(ts);

跟隨

        TabSpec ts;

        ts = th.newTabSpec("tag1");
        ts.setContent(R.id.tab1);
        ts.setIndicator("Tab one");
        th.addTab(ts);

        ts = th.newTabSpec("tag2");
        ts.setContent(R.id.tab2);
        ts.setIndicator("Tab two");
        th.addTab(ts);

        ts = th.newTabSpec("tag3");
        ts.setContent(R.id.tab3);
        ts.setIndicator("Tab Three ");
        th.addTab(ts);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM