0

我已经创建了标签作为视图。但是我没有找到关于如何操作这些视图的任何信息。例如,如果我想显示一个列表视图和一个窗体(tablelayout)在另一个。我可以为每个标签分别设计布局吗?更重要的是,我在哪里包含关于每个选项卡的Java代码?如何处理标签作为视图?

这里是我的Java代码:

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


@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.tabworkentry); 

TabHost mTabHost = getTabHost(); 


    mTabHost.addTab(mTabHost.newTabSpec("top10").setIndicator("Top 10").setContent(R.id.Top_10)); 
    mTabHost.addTab(mTabHost.newTabSpec("billable").setIndicator("Billable").setContent(R.id.Billable)); 
    mTabHost.addTab(mTabHost.newTabSpec("product").setIndicator("Product").setContent(R.id.Product)); 
     mTabHost.addTab(mTabHost.newTabSpec("regular").setIndicator("Regular").setContent(R.id.General)); 

    mTabHost.setCurrentTab(3); 


} 

任何有点帮助表示赞赏。

回答

1

有把内容和处理标签的方式主要有两种:1。 您创建活动,并把活动的tabHost 2.你处理的内容,在您添加相同的代码标签(打击所有安装的东西) 我总是用第二个方法,因为我的代码是有组织的,所以它不是那么大......

下面是一个简单的布局,与内容作为另一个布局...

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

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

     <LinearLayout 
      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/checkall_tab" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 

        <include 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         layout="@layout/checkall" /> 
       </LinearLayout> 

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

        <include 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         layout="@layout/viewall" /> 
       </LinearLayout> 

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

        <include 
         android:layout_width="match_parent" 
         android:layout_height="match_parent" 
         layout="@layout/programs" /> 
       </LinearLayout> 

      </FrameLayout> 
     </LinearLayout> 
    </TabHost> 

</LinearLayout> 
+0

好的。现在,如果我使用第二种方法,我可以为我的选项卡使用单独的布局,还是必须将所有内容都包含在单个布局中,例如我的活动? – Harsh

+0

yourTabSpec.setContent(R.id.the_id_of_your_tab); –

+0

要放在tabView中的“布局”必须在选项卡主机的布局中指定...查看它是如何做到的 –

0

我用Android 4.0 tabView制作了这段代码。尝试了解它... 你得实例化TabHost,然后用这个对象创建标签。 然后将该选项卡添加到TabHost。 要通过id查找视图,您不需要在正在访问的选项卡上传递“视图”。

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

    tabHost = (TabHost) findViewById(R.id.tabhost); 
    tabHost.setup(); 

    checkAll = tabHost.newTabSpec("checkAll"); 
    viewAll = tabHost.newTabSpec("viewAll"); 
    runProgram = tabHost.newTabSpec("runProgram"); 

    viewAll.setContent(R.id.view_tab); 
    viewAll.setIndicator("View All"); 
    tabHost.addTab(viewAll); 

    checkAll.setContent(R.id.checkall_tab); 
    checkAll.setIndicator("Check All"); 
    tabHost.addTab(checkAll); 

    runProgram.setContent(R.id.run_program); 
    runProgram.setIndicator("Run program"); 
    tabHost.addTab(runProgram); 
} 
+0

我做了同样的事情ING。我不是吗?我的问题是现在假设我想'view_tab'作为我的列表视图然后我在哪里写我的java代码相关的选项卡? – Harsh