2010-09-16 141 views
6

我正在开发带有选项卡式布局的Android应用程序。我已经知道它没有产生像Google's tutorial suggested这样的新活动,但是我只是在点击每个标签时努力让我的内容显示。目前它只显示黑色,无论选项卡处于活动状态。Android选项卡式布局setContent

以下是我在其最新的迭代代码:

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中

我只包括这两种,它们都是完全相同的同为LinearLayout中的android适当的标识: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> 

而这里的选项卡之间切换时,我得到什么的截图:

订单标签

alt text

位置标签

alt text

对于模拟器和我刚刚买到的三星Galaxy都是如此,我非常推崇顺便提一下,有什么想法?

回答

5

我想通了,这是一个简单的参数除了main.xml中的文件:

的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: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> 

通知的的LinearLayout,现在包括参数android:方向。之前它所做的是将其他屏幕向右推。现在它的工作原理应该如此。

注意:在明天的办公室,我可以测试这是否仍然允许我旋转手机并使其在横向方向上正确显示。如果没有,那么我可能必须为水平创建一个单独的xml文件,除非有人对此有深刻的了解。

+0

+1为答案和一个非常好的解释。 – Subby 2014-02-24 12:05:57

相关问题