سؤال

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?

Here's my java code:

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

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. try understanding it... You gotta instantiate the TabHost, then create tabs with this object. And then add the tab to TabHost. To findViews by id, you don't need to pass the "view" on the tab that you are accessing.

@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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top