如何在屏幕android底部的Tab布局中打开新活动

时间:2014-01-01 06:34:29

标签: java android android-layout tabs

我已经创建了一个Tab布局,它位于屏幕的底部,我正在使用更高版本的android ICS,我将发布标签布局的屏幕截图,我已经在底部创建了三个标签,现在我想要创建我的每个标签都有一个单独的活动,我尝试了很多但是没有成功,我也会发布我的代码。

public class MainActivity extends Activity  implements OnTabChangeListener, OnRefreshListener, OnScrollListener {

        private TabHost mTabHost;
        public static final String TAB_1 = "Open";
        public static final String TAB_2 = "Approved";
        public static final String TAB_3 = "Decline";
        private int mCurrentTab;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mTabHost = (TabHost) findViewById(android.R.id.tabhost);
            mPullToRefreshAttacher = PullToRefreshAttacher.get(this);
            mPullToRefreshLayout= (PullToRefreshLayout)findViewById(R.id.tab_1);
            setupTabs();
            mTabHost.setOnTabChangedListener(this);
            mTabHost.setCurrentTab(mCurrentTab);
        }

        private void setupTabs() {
            mTabHost.setup(); // you must call this before adding your tabs!
            mTabHost.addTab(newTab(TAB_1, R.id.listview1));
            mTabHost.addTab(newTab(TAB_2, R.id.listview2));
            mTabHost.addTab(newTab(TAB_3, R.id.listview3));             
        }

        private TabSpec newTab(String tag, int tabContentId) {

            Log.i("tag", tag);//fff498
            Log.i("tabContentId", ""+tabContentId);//fff498

             TabSpec spec = mTabHost.newTabSpec(tag);
             spec.setContent(tabContentId);

            if(tag.equalsIgnoreCase("Open")){
                 spec.setIndicator("", getResources().getDrawable(R.drawable.feed));
                 spec.setContent(tabContentId);

            }else if(tag.equalsIgnoreCase("Approved")){
                spec.setIndicator("", getResources().getDrawable(R.drawable.trend));
                spec.setContent(tabContentId);

            }else if(tag.equalsIgnoreCase("Decline")){
                spec.setIndicator("", getResources().getDrawable(R.drawable.popularr));
                spec.setContent(tabContentId);

            }

            for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
            {
                mTabHost.getTabWidget().getChildAt(0).setBackgroundColor(Color.parseColor("#fff498"));

            }
            return spec;
        }

        public void onTabChanged(String tabId) {

            for(int i=0;i<mTabHost.getTabWidget().getChildCount();i++)
            {
                mTabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.WHITE);
            }
            if (TAB_1.equals(tabId)) {
               // updateTab(R.id.tab_1);
                mCurrentTab = 0;
                //Toast.makeText(getApplicationContext(), "1No Guest list for Today", Toast.LENGTH_SHORT).show();
                mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
            }
            if (TAB_2.equals(tabId)) {
                //updateTab(R.id.tab_2);
                 mCurrentTab = 1;
                 //Toast.makeText(getApplicationContext(), "2No Guest list for Today", Toast.LENGTH_SHORT).show();
                 mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
            }
            if (TAB_3.equals(tabId)) {
                //updateTab(R.id.tab_2);
                 mCurrentTab = 2;
                // Toast.makeText(getApplicationContext(), "3No Guest list for Today", Toast.LENGTH_SHORT).show();
                 mTabHost.getTabWidget().getChildAt(mTabHost.getCurrentTab()).setBackgroundColor(Color.parseColor("#fff498"));
            }
        }
    }

布局XML

<?xml version="1.0" encoding="utf-8"?>
<TabHost  xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="8dp"
    android:paddingTop="10dp"
    android:paddingRight="8dp"
    android:background="#ffffff"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@android:id/tabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
         android:layout_weight="1" >

        <uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tab_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


         <ListView
        android:id="@+id/listview1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />

         </uk.co.senab.actionbarpulltorefresh.library.PullToRefreshLayout>
          <ListView
        android:id="@+id/listview2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

           <ListView
        android:id="@+id/listview3"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
         />

    </FrameLayout>

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

        android:layout_weight="0" >
    </TabWidget>
  </LinearLayout>

 </TabHost>

enter image description here

2 个答案:

答案 0 :(得分:1)

您只需在添加标签时设置Intent,如下所示:

  // Tab for Photos
    TabSpec photospec = tabHost.newTabSpec("Photos");
    // setting Title and Icon for the Tab
    photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
    Intent photosIntent = new Intent(this, PhotosActivity.class);
    photospec.setContent(photosIntent);

有关详情,请访问以下链接:Android tab layout tutorial

答案 1 :(得分:0)

试试这个..

private void setUpTabs() {

    TabHost _tabHost = (TabHost) findViewById(android.R.id.tabhost);
    // tab 1
    TabHost.TabSpec _tab_spec1 = _tabHost.newTabSpec("first_screen_tag");
    _tab_spec1.setIndicator("", getResources().getDrawable(R.drawable.feed));
    Intent _screen1 = new Intent(this, FirstActivity.class);
    _tab_spec1.setContent(_screen1);

    // tab 2
    TabHost.TabSpec _tab_spec2 = _tabHost.newTabSpec("second_screen_tag");
    _tab_spec2.setIndicator("", getResources().getDrawable(R.drawable.trend));
    Intent _screen2 = new Intent(this, SecondActivity.class);
    _tab_spec2.setContent(_screen2);

    // tab 3
    TabHost.TabSpec _tab_spec3 = _tabHost.newTabSpec("third_screen_tag");
    _tab_spec3.setIndicator("", getResources().getDrawable(R.drawable.popularr));
    Intent _screen3 = new Intent(this, ThirdActivity.class);
    _tab_spec3.setContent(_screen3);

            // Adding all the tabs to the TabView
    _tabHost.addTab(_tab_spec1);
    _tabHost.addTab(_tab_spec2);
    _tabHost.addTab(_tab_spec3);
}