zoukankan      html  css  js  c++  java
  • 【Android】选项卡使用

    一,创建三个Activity类

    MainActiviey.java

    package tianshuai.home_page;
    
    import android.app.Activity;
    import android.app.ActivityGroup;
    import android.content.Intent;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TabHost;
    import android.widget.TabHost.TabSpec;
    
    public class MainActivity extends ActivityGroup implements View.OnClickListener
    {
        public static TabHost tab_host;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            createTab();
        }
        
         public  void createTab()
        {
            tab_host = (TabHost) findViewById(R.id.tab_host);
            tab_host.setup(this.getLocalActivityManager());
            
            tab_host.setOnClickListener(this);
    
            TabSpec ts1 = tab_host.newTabSpec("手机酷站");   
            ts1.setIndicator("正在播放", getResources().getDrawable(R.drawable.find));
            ts1.setContent(new Intent(this, cellphone_web.class));
           
            tab_host.addTab(ts1);
            
            TabSpec ts2 = tab_host.newTabSpec("软件游戏");
            ts2.setIndicator("本地曲库", getResources().getDrawable(R.drawable.history));
            ts2.setContent(new Intent(this, software.class));
           
            tab_host.addTab(ts2);
            tab_host.setCurrentTab(1);       
        }
         
        public void onClick(View arg0) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
         
    }

    二,cellphone_web.java 跟 software.java均为继承 Activity 的空类就可以

    三,main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
            
        <TabHost 
            android:id="@+id/tab_host"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent">
            <LinearLayout 
                 android:orientation="vertical"
                 android:layout_width="fill_parent" 
                 android:layout_height="fill_parent"
                 android:padding="5dp">
                <FrameLayout 
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:layout_weight="1" />
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0" />
            </LinearLayout>
        </TabHost>
    </LinearLayout>
    

    注意:TabWidget和FrameLayout 有不同的ID命名空间android:id="@android:id/idnames",这个是必须的因此TabHost才能自动找到它,Activity需要继承TabActivity。

    如果想让选项卡在上方的话:

    <FrameLayout 
                    android:id="@android:id/tabcontent"
                    android:layout_width="fill_parent" 
                    android:layout_height="wrap_content"
                    android:padding="5dp"
                    android:layout_weight="0" />
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1" />
    内容想在上边,TabWidget 必须与 FrameLayout 换换位置


  • 相关阅读:
    Error和Exception的区别
    当try和finally都包含return时的执行顺序
    String,StringBuffer处理字符串的区别
    使用idea对XML的增删改查
    IO流,字节流复制文件,字符流+缓冲复制文件
    MySQL同步故障:" Slave_SQL_Running:No" 主从同步的从表进行了写操作
    常用MQ的对比冷知识
    Redis-避免缓存穿透
    Docker容器与虚拟化技术——部署KVM虚拟化平台
    HTML日记 第三篇 关于图片的冷知识(附带一些浮动的基础知识)
  • 原文地址:https://www.cnblogs.com/secbook/p/2655024.html
Copyright © 2011-2022 走看看