zoukankan      html  css  js  c++  java
  • Android中TabHost切换不同的Activity

    TabHost在很多应用都会使用到,有时候在TabHost添加的Tab中设置view不能满足需求,因为在view中添加如PreferenceActivity相当困难.

    之前在一个应用中需要实现使用TabHost中在多个Tab切换不同的Activity.一个是日志列表(ListActivity),一个是应用设置页面( PreferenceActivity )

    先上效果图

    上图是日志列表页面,是ListActivity

    上图是设置页面,是一般的PreferenceActivity

    开发流程 :
    1, 编写不同的Activity,用于在TabHost中点击不同的Tab时进行切换(以下是两个例子,因为不是该博文的主要内容所以省略)
    1-1, 编写LogActivity,该类是ListActivity,用于显示日志内容
    1-2, 编写SettingActivity,该类是PreferenceActivity,应用的设置页面,编写方式与PreferenceActivity一样.

    2, 编写主页面(MainActivity),该类是应用的入口类,在该类定义TabHost并添加相应的Activity.

    // 该类需要继承ActivityGroup
    public class MainActivity extends ActivityGroup {
      private TabHost mTabHost;
     
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        // 设置TabHost
        initTabs();
      }
     
      private void initTabs() {
        mTabHost = (TabHost) findViewById(R.id.tabhost);
        mTabHost.setup(this.getLocalActivityManager());
        // 添加日志列表的tab,注意下面的setContent中的代码.是这个需求实现的关键
        mTabHost.addTab(mTabHost.newTabSpec("tab_log")
          .setIndicator("日志",getResources().getDrawable(R.drawable.ic_tab_log))
          .setContent(new Intent(this, LogActivity.class)));
     
        // 添加应用设置的tab,注意下面的setContent中的代码.是这个需求实现的关键
        mTabHost.addTab(mTabHost.newTabSpec("tab_setting")
          .setIndicator("设置",getResources().getDrawable(R.drawable.ic_tab_settings))
          .setContent(new Intent(this, SettingActivity.class)));
        mTabHost.setCurrentTab(1);
      }
    }

    3, 编写main.xml,MainActivity的layout的xml配置文件

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	>
    	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    		android:orientation="vertical" android:layout_width="fill_parent"
    		xmlns:app="http://schemas.android.com/apk/res/rbase.app.nowscore"
    		android:layout_height="fill_parent" 
                    android:layout_above="@+id/adLayout">
    		<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    			android:id="@+id/tabhost" android:layout_width="fill_parent"
    			android:layout_height="wrap_content">
    			<LinearLayout android:orientation="vertical"
    				android:layout_width="fill_parent" 
                                    android:layout_height="wrap_content">
    				<TabWidget android:id="@android:id/tabs"
    					android:layout_width="fill_parent" 
                                            android:layout_height="wrap_content" />
    				<FrameLayout android:id="@android:id/tabcontent"
    					android:layout_width="fill_parent" 
                                            android:layout_height="fill_parent" />
    			</LinearLayout>
    		</TabHost>
    	</LinearLayout>
    </RelativeLayout>
    

      补充:
    当在不同的Tab中按下menu按钮,会以当前的Activity为准,即在SettingActivity时按下menu按钮后会触发SettingActivity的事件,不是主页面的事件.

     

     

  • 相关阅读:
    列出本年度所有星期天的日期
    批量与快速禁用用户
    显示本月每一天日期
    ASP.NET + VB.NET + SQL小网站程序
    重设Syteline sa帐户密码
    ASP.NET系统 + Access数据库
    2的倍数相加后如何还原
    根据Forms名找出其所归属的权限组
    401 Unauthorized:access is denied due to invalied credentials
    The remote server returned an error: (404) Not Found
  • 原文地址:https://www.cnblogs.com/gzggyy/p/2471063.html
Copyright © 2011-2022 走看看