2010-06-04 36 views
46

Hướng dẫn TabWidget dành cho nhà phát triển Android cho biết:Ví dụ về việc sử dụng tab Android có Chế độ xem thay vì Hoạt động?

"Bạn có thể triển khai nội dung tab theo một trong hai cách: sử dụng tab để hoán đổi Chế độ xem trong cùng một Hoạt động hoặc sử dụng các tab để thay đổi giữa các hoạt động hoàn toàn riêng biệt. "

Hướng dẫn tiếp tục để minh họa cách bạn có thể sử dụng các tab có Hoạt động riêng biệt. Tôi không thể tìm thấy ví dụ về việc sử dụng các tab có Chế độ xem khác nhau trong cùng một Hoạt động. Tôi thà không tái phát minh ra bánh xe đặc biệt này, vì vậy tôi hy vọng một người nào đó ở đây biết làm thế nào điều này được thực hiện và có thể đầu mối tôi.

+3

Vấn đề với điều này là nó mở rộng TabActivity không được chấp nhận. –

Trả lời

40

Tôi nghĩ rằng trong phương pháp .setContent của mỗi tab bạn vượt qua trong giao diện bạn muốn sử dụng:

TabHost.TabSpec spec1 = tabs.newTabSpec("tag1"); 
spec1.setContent(R.id.AnalogClock01); 
spec1.setIndicator("Analog Clock"); 

Dưới đây là một ví dụ tôi tìm thấy một lúc quay lại:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 

    <TabHost android:id="@+id/TabHost01" android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    <TabWidget android:id="@android:id/tabs" android:layout_width="wrap_content" android:layout_height="wrap_content" /> 
    <FrameLayout android:id="@android:id/tabcontent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="65px"> 
     <AnalogClock android:id="@+id/AnalogClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></AnalogClock> 
     <DigitalClock android:text="DigitalClock01" android:id="@+id/DigitalClock01" android:layout_width="wrap_content" android:layout_height="wrap_content"></DigitalClock> 
    </FrameLayout> 
    </TabHost> 
</LinearLayout> 

Và mã Java cho ví dụ này là như sau:

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TabHost; 

public class tabexample extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     TabHost tabs = (TabHost)findViewById(R.id.TabHost01); 

     tabs.setup(); 

     TabHost.TabSpec spec1 = tabs.newTabSpec("tag1"); 

     spec1.setContent(R.id.AnalogClock01); 
     spec1.setIndicator("Analog Clock"); 

     tabs.addTab(spec1); 

     TabHost.TabSpec spec2 = tabs.newTabSpec("tag2"); 
     spec2.setContent(R.id.DigitalClock01); 
     spec2.setIndicator("Digital Clock"); 

     tabs.addTab(spec2); 
    } 
} 
+0

Nếu tôi đã thực hiện một số "tác vụ" trên mỗi chế độ xem thì sao? Ví dụ, nếu tôi phân tích cú pháp một số XML trên View1 và hiển thị dữ liệu sau khi truy vấn cơ sở dữ liệu SQLITE trên View2? Viết các hoạt động riêng biệt cho mỗi tab là giải pháp duy nhất trong trường hợp này? –

Các vấn đề liên quan