android开发 - TabHost使用

来源:互联网 发布:适合业务员的软件 编辑:程序博客网 时间:2024/05/17 06:16
使用TabHost实现标签页


使用场景,如:通讯录,QQ,等通讯软件都有使用


TabHost是一个标签页的集合




主布局如下:


<!-- 外部一个TabHost集合 -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tabhost">


    <!-- TabHost中集合布局是使用线性布局 -->
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        
        <!-- 布局中的标签切换卡,使用TabWidget来切换 ,这里的id是系统给定的名称,所以使用系统给定的tabs为id -->
        <TabWidget
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"/>
        
        <!-- 布局中的每个标签内容使用桢布局 ,然后每个切换卡的内容使用LinearLayout布局 ,那么显示的内容就在LinearLayout中显示 -->
        <!-- 它的id是系统给定的tabcontent为id,调用如下,也可以查看源代码 -->
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@android:id/tabcontent"> 
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:id="@+id/page1">
                
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="这是第一个标签页内容"/> 
            </LinearLayout>
            
             <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:id="@+id/page2">
                
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="这是第二个标签页内容"/> 
            </LinearLayout>
            
              <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" 
                android:id="@+id/page3">
                
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="这是第三个标签页内容"/> 
            </LinearLayout>
            
        </FrameLayout> 
    </LinearLayout> 
</TabHost>




自定义页面布局
<?xml version="1.0" encoding="utf-8"?>
<!-- 这里是自定义的标签页 ,只是给TabWidget自定义--> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:background="@drawable/tab_bj"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="18sp"
        android:id="@+id/name"/> 
</LinearLayout>


自定义布局的标签页的事件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下时 -->
    <item android:state_pressed="true" android:drawable="@drawable/a1" />
    <!-- 选中时 -->
    <item android:state_selected="true" android:drawable="@drawable/a2" />
    <!-- 默认 -->
    <item android:drawable="@drawable/ic_launcher" />
</selector>






java代码


public class MainActivity extends Activity {
private TabHost tabHost;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //找到TabHost对象
        tabHost = (TabHost)this.findViewById(R.id.tabhost);
        //设置TabWidget,和TabContent的
        tabHost.setup();
        
        //以下的标签页都是系统默认的,但是我们可以自定义标签页,也就是我们写一个View
        //设置标签页1
        TabSpec tabSpec = tabHost.newTabSpec("page1");
        //这是自定义
        tabSpec.setIndicator(createTabView("首页"));
        //这是系统默认的
        //tabSpec.setIndicator("首页",getResources().getDrawable(R.drawable.ic_launcher));
        tabSpec.setContent(R.id.page1);
        tabHost.addTab(tabSpec);
        
        //设置标签页1
        tabSpec = tabHost.newTabSpec("page1");
        //这是自定义
        tabSpec.setIndicator(createTabView("第二页"));
        //这是系统默认的
        //tabSpec.setIndicator("第二页",getResources().getDrawable(R.drawable.ic_launcher));
        tabSpec.setContent(R.id.page2);
        tabHost.addTab(tabSpec);
        
        //设置标签页1
        tabSpec = tabHost.newTabSpec("page1");
        //这是自定义
        tabSpec.setIndicator(createTabView("第三页"));
        //这是系统默认的
        //tabSpec.setIndicator("第三页",getResources().getDrawable(R.drawable.ic_launcher));
        tabSpec.setContent(R.id.page3);
        tabHost.addTab(tabSpec);
     
        //默认载入时,是在第一页
        tabHost.setCurrentTab(0);
    }




    private View createTabView(String name) {
View tabView = getLayoutInflater().inflate(R.layout.tab, null);
TextView tv = (TextView)tabView.findViewById(R.id.name);
tv.setText(name);
return tabView;

}





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 信封封条破损了怎么办 奶粉罐子扁了怎么办 奶粉邮寄爆罐怎么办 奶粉打开爆罐怎么办 海关包裹被扣怎么办 天猫未收货显示签收怎么办 物流显示包裹破损怎么办 快递寄鞋盒破损了怎么办 邮寄东西碎了怎么办 快递屏幕碎了怎么办 快递东西破了怎么办 收快递东西损坏怎么办 快递寄新鲜东西怎么办 寄东西损坏了怎么办 顺丰写错电话号码已签收怎么办 快递包装扔了怎么办 淘宝快递损坏了怎么办 买家寄过来破损怎么办 卖家发假单号自动签收怎么办 ems快递包裹破损怎么办 微波炉外包装扔掉退货怎么办 交警判决不服该怎么办 如果判决不服该怎么办 学不会看卡尺怎么办 我学不会卡尺怎么办 卡尺表针歪了怎么办 电喷摩托车没电怎么办 话筒插头坏了怎么办 电柜接线看不懂怎么办 发动机缸盖渗油怎么办 吃杏吃多了肚子难受怎么办 住院单据丢了怎么办 医院单据丢了怎么办 购房单据丢了怎么办 废电瓶回收手续怎么办 ai置入图片过大怎么办 ps 弄错纸张大小怎么办 屁股沟发炎红肿怎么办 房间里床太大怎么办 房间太小放不下桌子怎么办 没衣柜一堆衣服怎么办