2010-09-16 34 views
6

Tôi đang phát triển một ứng dụng Android có bố cục theo thẻ. Tôi đã có nó đến nơi nó không đẻ trứng một hoạt động mới như Google's tutorial suggested, tuy nhiên tôi chỉ làm điều này trong một nỗ lực để có được nội dung của tôi để hiển thị khi nhấp vào mỗi tab. Hiện tại, nó chỉ hiển thị màu đen bất kể tab nào đang hoạt động.Bố cục tab Android được thiết lậpContent

Sau đây là mã của tôi trong phiên bản mới nhất của nó:

Main.java

public class Main extends TabActivity { 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Resources res = getResources(); 
     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 

     // add orders tab 
     spec = tabHost.newTabSpec("orders").setIndicator("Orders", 
          res.getDrawable(R.drawable.flash_36)) 
          .setContent(R.id.ordersLayout); 
     tabHost.addTab(spec); 

     // add positions tab 
     spec = tabHost.newTabSpec("positions").setIndicator("Positions", 
          res.getDrawable(R.drawable.small_tiles_36)) 
          .setContent(R.id.positionsLayout); 
     tabHost.addTab(spec); 

     // add strategies tab 
     spec = tabHost.newTabSpec("strategies").setIndicator("Strategies", 
          res.getDrawable(R.drawable.cards_36)) 
          .setContent(R.id.strategiesLayout); 
     tabHost.addTab(spec); 

     // add account tab 
     spec = tabHost.newTabSpec("account").setIndicator("Account", 
          res.getDrawable(R.drawable.seal_36)) 
          .setContent(R.id.accountLayout); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(1); 
    } 

} 

main.xml

<?xml version="1.0" encoding="utf-8"?> 


<TabHost android:id="@android:id/tabhost" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      xmlns:android="http://schemas.android.com/apk/res/android"> 

      <LinearLayout android:id="@+id/mainLayout" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:padding="5dp"> 

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

       <FrameLayout android:id="@android:id/tabcontent" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:background="#fff" 
          android:padding="5dp"> 

          <include layout="@layout/orders"/> 
          <include layout="@layout/positions"/> 
          <include layout="@layout/strategies"/> 
          <include layout="@layout/account"/> 

       </FrameLayout> 

      </LinearLayout> 

</TabHost> 

Account.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:id="@+id/accountLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:text="Account Information" 
     android:id="@+id/accountLabel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

    </TextView> 

</LinearLayout> 

Orders.xml

Tôi chỉ bao gồm hai, họ là tất cả các chính xác cùng với id thích hợp cho android của LinearLayout: tham số id.

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    android:id="@+id/ordersLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#fff" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:text="Working Orders" 
     android:id="@+id/ordersLabel" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

    </TextView> 

</LinearLayout> 

Và đây là một ảnh chụp màn hình của những gì tôi nhận được khi chuyển đổi giữa các tab:

Orders Tab

alt text

Positions Tab

alt text

Điều này đúng cho cả trình giả lập và Galaxy Samsung tôi vừa nhận được, và tôi rất coi trọng bằng cách này, mọi ý tưởng?

Trả lời

5

I figured it out, và đó là một bổ sung tham số đơn giản để file main.xml:

main.xml (sửa đổi)

<?xml version="1.0" encoding="utf-8"?> 


<TabHost android:id="@android:id/tabhost" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      xmlns:android="http://schemas.android.com/apk/res/android"> 

      <LinearLayout android:id="@+id/mainLayout" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:orientation="vertical" 
          android:padding="5dp"> 

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

       <FrameLayout android:id="@android:id/tabcontent" 
          android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
          android:background="#fff" 
          android:padding="5dp"> 

          <include layout="@layout/orders"/> 
          <include layout="@layout/positions"/> 
          <include layout="@layout/strategies"/> 
          <include layout="@layout/account"/> 

       </FrameLayout> 

      </LinearLayout> 

</TabHost> 

Thông báo các LinearLayout, nó bây giờ bao gồm thông số android: orientation. Những gì nó đã làm trước khi đẩy mọi thứ khác ra khỏi màn hình ở bên phải. Bây giờ nó hoạt động như nó cần.

Lưu ý: Tại văn phòng ngày mai tôi có thể kiểm tra xem điều này vẫn cho phép tôi xoay điện thoại và hiển thị điện thoại chính xác theo hướng ngang hay không. Nếu không, thì tôi có lẽ sẽ phải tạo một tệp xml riêng biệt cho chiều ngang, trừ khi ai đó có hiểu biết sâu sắc về điều này tất nhiên.

+0

+1 cho câu trả lời và giải thích rất tốt. – Subby

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