zoukankan      html  css  js  c++  java
  • Android TabHost的使用(Tab为Layout)

    Android TabHost的使用,这里采用继承TabActivity的方法。

    这里分别定制三个Tab,分别为american.xml, chinese.xml, japanese.xml三个Layout。

    american.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="American1" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="American2" />
    
    </LinearLayout>
    

     chinese.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chinese button 1" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Chinese button 2" />
    
    </LinearLayout>
    

      japanese.xml文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        
            <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Japanese button 1" />
    
            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Japanese button 2" />
    
    </LinearLayout>
    

    三个Layout对应的java文件为AmericanActivity.java, ChinaActivity.java, JapanActivity.java

    AmericanActivity.java文件

    public class AmericanActivity extends Activity {
    
    	 @Override
    	    protected void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
    	        setContentView(R.layout.american);
    	     
    	    }
    }
    

      ChinaActivity.java文件

    public class ChinaActivity extends Activity {
    
    	 @Override
    	    protected void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
    	        setContentView(R.layout.chinese);
    	       
    	    }
    }
    

      JapanActivity.java文件

    public class JapanActivity extends Activity {
    
    	 @Override
    	    protected void onCreate(Bundle savedInstanceState) {
    	        super.onCreate(savedInstanceState);
    	        setContentView(R.layout.japanese);
    	      
    	    }
    }
    

      

    MainActivity.java 继承TabActivity。

    public class MainActivity extends TabActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //setContentView(R.layout.activity_main);
            Resources resources = getResources();
            TabHost tabHost = getTabHost();
            
            TabHost.TabSpec spec;
            
            Intent intent = new Intent(this,AmericanActivity.class );
            spec = tabHost.newTabSpec("American");
            spec.setIndicator("Ameican Tab"); 
            spec.setContent(intent);
            tabHost.addTab(spec);
            
            Intent intent2 = new Intent(this,ChinaActivity.class );
            spec = tabHost.newTabSpec("China");
            spec.setIndicator("China Tab") 
            spec.setContent(intent2);
            tabHost.addTab(spec);
            
            Intent intent3 = new Intent(this,JapanActivity.class );
            spec = tabHost.newTabSpec("Japanese");
            spec.setIndicator("Japanese Tab"); 
            spec.setContent(intent3);
            tabHost.addTab(spec);
            
            tabHost.setCurrentTab(1);
        }
    
        
    }
    

      注意:在AndroidManifest文件中加入下面三行代码。

            <activity android:name="com.example.app1.AmericanActivity" android:label="@string/app_name"></activity>
            <activity android:name="com.example.app1.JapanActivity" android:label="@string/app_name"></activity>
            <activity android:name="com.example.app1.ChinaActivity" android:label="@string/app_name"></activity>
    

     效果图:

    作者:Work Hard Work Smart
    出处:http://www.cnblogs.com/linlf03/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    JS函数浅析(一)
    H5_canvas与svg
    h5+js视频播放器控件
    【BZOJ3622】已经没有什么好害怕的了
    【9.29 模拟】T3 小清新最优化(easy)
    9.27模拟
    9.26 模拟
    4062 -- 【清华集训2012】串珠子
    【SNOI2017】炸弹
    P3216 [HNOI2011]数学作业
  • 原文地址:https://www.cnblogs.com/linlf03/p/2963592.html
Copyright © 2011-2022 走看看