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 换换位置


  • 相关阅读:
    forms组件、cookie与session
    choices参数、MTV与MVC模型、Ajax、序列化组件、sweetalert搭建页面、自定义分页器
    Django 常用字段,数据库查询优化 only与defer
    django配置代码
    django 模板层、模型层
    Django 请求生命周期、路由层
    centos6.8安装httpd后无法访问
    初次认识dedecms和帝国cms内容管理系统
    遇到一个json解码失败的问题
    关于apache配置映射端口
  • 原文地址:https://www.cnblogs.com/secbook/p/2655024.html
Copyright © 2011-2022 走看看