zoukankan      html  css  js  c++  java
  • Android 多个Activity选项卡实现

    Tab选项卡是一个非常方便的组件

    今天查询资料知道了Android 多个Activity选项卡实现

    本篇文章来源于好岸园it技术学习网 (http://www.hopean.com

    原文链接:http://www.hopean.com/devlop/

    创建多个Activity比如叫FirstActivity,SecondActivity,ThirdActivity

    由于是简单实现, FirstActivity,SecondActivity,ThirdActivity三者代码基本相同。

    下面以FirstActivity为例

    package com.example.androidtabselector;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.TextView;
    
    public class FirstActivity extends Activity {
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		TextView textview = new TextView(this);
    		textview.setText("这是Tab1"); 
    		setContentView(textview);
    		}  
    }
    


     

    修改main.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:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >
           
    
            <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"
                android:padding="5dp" />
       
        </LinearLayout>
    
    
    </TabHost>
    


     

    之后新建一个TabDemoActivity.java文件,详细代码如下:

    package com.example.androidtabselector;
    
    import android.app.TabActivity;
    import android.content.Intent;
    import android.content.res.Resources;
    import android.os.Bundle;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;  
    
    
    public class TabDemoActivity extends TabActivity {
    	/** Called when the activity is first created. */ 
    	@Override
    
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);//这里使用了上面创建的xml文件(Tab页面的布局)
    		Resources res = getResources(); // Resource object to get Drawables
    		TabHost tabHost = getTabHost();  // The activity TabHost
    		TabSpec spec;
    		Intent intent;  // Reusable Intent for each tab 
    		//第一个Tab
    		intent = new Intent(this,FirstActivity.class);
    		//新建一个Intent用作Tab1显示的内容
    		spec = tabHost.newTabSpec("tab1")//新建一个 Tab      
    				.setIndicator("Tab1", res.getDrawable(android.R.drawable.ic_menu_camera))//设置名称以及图标  25. 
    				.setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx
    		tabHost.addTab(spec);//添加进tabHost 
    		//第二个Tab  29. 
    		intent = new Intent(this,SecondActivity.class);//第二个Intent用作Tab1显示的内容
    		spec = tabHost.newTabSpec("tab2")//新建一个 Tab
    				.setIndicator("Tab2", res.getDrawable(android.R.drawable.ic_menu_edit))//设置名称以及图标
    				.setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx
    		tabHost.addTab(spec);//添加进tabHost 
    		//第三个Tab  
    		intent = new Intent(this,ThirdActivity.class);//第二个Intent用作Tab1显示的内容 
    		spec = tabHost.newTabSpec("tab2")//新建一个 Tab 
    				.setIndicator("Tab3", res.getDrawable(android.R.drawable.ic_menu_help))//设置名称以及图标 
    				.setContent(intent);//设置显示的intent,这里的参数也可以是R.id.xxx 
    		tabHost.addTab(spec);//添加进tabHost 
    		tabHost.setCurrentTab(0);//设置当前的选项卡,这里为Tab1 
    		}   
    }
    
    


     

    最后修改AndroidManifest.xml文件,详细代码如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.androidtabselector"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="16" />
    
        <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
    <!--         <activity -->
    <!--             android:name="com.example.androidtabselector.MainActivity" -->
    <!--             android:label="@string/app_name" > -->
    <!--             <intent-filter> -->
    <!--                 <action android:name="android.intent.action.MAIN" /> -->
    
    <!--                 <category android:name="android.intent.category.LAUNCHER" /> -->
    <!--             </intent-filter> -->
    <!--         </activity> -->
            <activity
            android:name=".TabDemoActivity"
            android:label="@string/app_name" >           
            <intent-filter>            
                <action android:name="android.intent.action.MAIN" />               
                <category android:name="android.intent.category.LAUNCHER" />           
            </intent-filter>    
        </activity>   
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >            
            <intent-filter>                
                <action android:name="android.intent.action.MAIN" />               
                <category android:name="android.intent.category.LAUNCHER" />          
            </intent-filter>    
        </activity>   
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >          
            <intent-filter>                
                <action android:name="android.intent.action.MAIN" />                
                <category android:name="android.intent.category.LAUNCHER" />       
            </intent-filter>
        </activity>
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" >          
            <intent-filter>                
                <action android:name="android.intent.action.MAIN" />                
                <category android:name="android.intent.category.LAUNCHER" />       
            </intent-filter>
        </activity>
        </application>
    
        
    </manifest>
    


     

    至此就可以实现简单的android 选项卡效果

     

    本篇文章来源于好岸园it技术学习网 (http://www.hopean.com原文链接:http://www.hopean.com/devlop/

     

    TabSelectorDemo 源码下载 http://www.hopean.com

  • 相关阅读:
    hdu 5170 GTY's math problem(水,,数学,,)
    hdu 5178 pairs(BC第一题,,方法不止一种,,我用lower_bound那种。。。)
    hdu 5179 beautiful number(构造,,,,)
    cf12E Start of the season(构造,,,)
    cf12D Ball(MAP,排序,贪心思想)
    cf 12C Fruits(贪心【简单数学】)
    cf 12B Correct Solution?(贪心)
    hdu 5171 GTY's birthday gift(数学,矩阵快速幂)
    hdu 5172 GTY's gay friends(线段树最值)
    cf 11D A Simple Task(状压DP)
  • 原文地址:https://www.cnblogs.com/hopeanCom/p/2858114.html
Copyright © 2011-2022 走看看