請問一下該如何修改 (TabActivity)

by oiffice
2009.05.19 12:09PM
請問一下該如何修改 (TabActivity)

應該如何讓tab裡面擁有多個button

但是彼此tab不會互相干擾

我目前狀況是

可以在第一個tab上顯示多個button

可是在第二個tab 會出現 第一個tab裡的button

這該怎麼解決?

=========以下為程式碼============

package com.android.demo.tabTest2;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;


public class TabTest2 extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        TabHost mTabHost = getTabHost();
       
        mTabHost.addTab(mTabHost.newTabSpec("tab_test1").setIndicator("", getResources().getDrawable(R.drawable.aa)).setContent(R.id.content1));
        mTabHost.addTab(mTabHost.newTabSpec("tab_test2").setIndicator("TAB 2").setContent(R.id.content2));
       
       
        mTabHost.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">
        <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="fill_parent">
            <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                         android:layout_width="fill_parent"
                         android:layout_height="fill_parent">
            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dip"
                android:layout_marginTop="25dip"
                android:text="a" />
            <Button
                android:id="@+id/button1_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/button1"
                android:layout_alignTop="@id/button1"
                android:layout_marginLeft="50dip"
                android:text="aaa"/>
            <Button
                android:id="@+id/button1_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/button1_1"
                android:layout_alignTop="@id/button1_1"
                android:layout_marginLeft="50dip"
                android:text="aaaa"/>
               
            <Button
                android:id="@+id/button1_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@id/button1"
                android:layout_marginLeft="25dip"
                android:layout_marginTop="25dip"
                android:text="b"/> 

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="25dip"
                android:layout_marginTop="25dip"
                android:text="c" />
                </RelativeLayout>
         </FrameLayout>
    </LinearLayout>
</TabHost>

4 則回應

  •  我現在有一個問題....

    我是使用以下的方法的...

     tabHost.addTab(tabHost.newTabSpec("tab1") .setIndicator("Tab1") .setContent(new Intent(this, Tab1.class)));

    tabHost.addTab(tabHost.newTabSpec("tab2") .setIndicator("Tab2") .setContent(new Intent(this, Tab2.class)));

    我在Tab1.class有一個button,想要按下button跑去第一個Tab2. 那Tab1.java要如何寫呢..

    謝謝..

    2009-11-08
  • 問題已經解決了 謝謝

    可以利用table的方法

    2009-05-29
  •  每一個tab用一個frame layout?

    你有先看一下API demo嗎 ?

    or

    Hello, Views

    歡迎一起討論Android http://tttt-note.blogspot.com/

     

    2009-05-20
    • anderson wrote:

       每一個tab用一個frame layout?

      你有先看一下API demo嗎 ?

      or

      Hello, Views

       

      Sorry ~  hello View裡面沒有Tab Orz

      科科 可以看一下Api demo 以及 Contacts的code (這我確定有Tab ~)

      之前我用的時候都是每一個Tab 自己一個Activity 所以沒有遇到這樣的問題@@


              tabHost.addTab(tabHost.newTabSpec("tab1")
                      .setIndicator("Tab1")
                      .setContent(new Intent(this, Tab1.class)));

              tabHost.addTab(tabHost.newTabSpec("tab2")
                      .setIndicator("Tab2")
                      .setContent(new Intent(this, Tab2.class)));      
       

      參考一下嚕

      歡迎一起討論Android http://tttt-note.blogspot.com/

       

      2009-05-20