• 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/
    欢迎任何形式的转载,未经作者同意,请保留此段声明!

  • 相关阅读:
    Mac 终端自动补全忽略大小写
    AtCoder ABC 155F Perils in Parallel
    AtCoder ABC 155E Payment
    AtCoder ABC 155D Pairs
    AtCoder ABC 154F Many Many Paths
    牛客 边界都是1的最大正方形大小
    牛客 打印N个数组整体最大的Top K
    牛客 数组中子数组的最大累乘积
    牛客 在数组中找到一个局部最小的位置
    牛客 子矩阵最大累加和问题
  • 原文地址:https://www.cnblogs.com/linlf03/p/2963592.html
Copyright © 2020-2023  润新知