简体   繁体   English

如何将选项卡作为视图处理?

[英]How to deal with tabs as views?

I've created tabs as views. 我已将标签创建为视图。 But I didn't find any info on how to manipulate theses views. 但是我没有找到有关如何操纵这些视图的任何信息。 For example if I want to display a listview in one and a form (tablelayout) in another. 例如,如果我想在一个视图中显示一个列表视图,而在另一个视图中显示一个表单(表格布局)。 Can I have separate layouts for each tab? 我可以为每个标签设置单独的布局吗? And more importantly where do I include java code regarding each tab? 而且更重要的是,我在哪里包含有关每个选项卡的Java代码?

Here's my java code: 这是我的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);


}

Any kinda help is appreciated. 任何帮助将不胜感激。

There are two main ways of putting content and dealing with tabs: 1. you create an activity, and put the activity in the tabHost 2. you deal with the content, in the same code you add tabs (blow all the setup stuff) I always use the 2nd way because my codes are organized, so it's not so big... 放置内容和处理选项卡的主要方法有两种:1.创建活动,然后将活动放置在tabHost中。2.使用相同的代码添加选项卡来处理内容(删除所有设置内容)总是使用第二种方式,因为我的代码是有组织的,所以不会太大...

Here is a sample layout, with the content as another layout... 这是一个示例布局,其中内容是另一个布局...

<?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>

I made this code using Android 4.0 tabView. 我使用Android 4.0 tabView编写了此代码。 try understanding it... You gotta instantiate the TabHost, then create tabs with this object. 尝试理解它。。。您必须实例化TabHost,然后使用此对象创建选项卡。 And then add the tab to TabHost. 然后将选项卡添加到TabHost。 To findViews by id, you don't need to pass the "view" on the tab that you are accessing. 要按ID查找Views,无需在访问的选项卡上传递“视图”。

@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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM