zoukankan      html  css  js  c++  java
  • Android TabHost的使用 顶部选项卡

    用TabHost 来实现顶部选项卡,上代码:activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <TabHost
            android:id="@+id/tabMenu"
            android:layout_width="match_parent"
            android:layout_height="0dp">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
    
                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
    
                    <LinearLayout
                        android:id="@+id/tab1"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/tab2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
    
                    </LinearLayout>
    
                    <LinearLayout
                        android:id="@+id/tab3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">
    
                    </LinearLayout>
                </FrameLayout>
            </LinearLayout>
        </TabHost>
    
    </android.support.constraint.ConstraintLayout>

    主方法MainActivity.java

    package action.sun.com.tabhost;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.TabHost;
    
    public class MainActivity extends AppCompatActivity {
    
        private TabHost tabhost;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //得到TabHost对象实例
            tabhost =(TabHost) findViewById(R.id.tabMenu);
    
            //调用 TabHost.setup()
            tabhost.setup();
            //创建Tab标签
            tabhost.addTab(tabhost.newTabSpec("one").setIndicator("红色").setContent(R.id.tab1));
            tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.tab2));
            tabhost.addTab(tabhost.newTabSpec("three").setIndicator("黄色").setContent(R.id.tab3));
    
            tabhost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
                @Override
                public void onTabChanged(String s) {
                    Log.d("xxx", "onTabChanged: ="+s);
                    if (s.equals("one")){
                        //可是让viewpage的视图显示出来
                        //viewPager.setCurrentItem(0);
                    }else if (s.equals("two")){
                        ////可是让viewpage的视图显示出来
                       // viewPager.setCurrentItem(1);
                    }
                }
            });
        }
    }

    代码很简单,实现效果:

  • 相关阅读:
    解决 未能为数据库 '数据库用户名' 中的对象 '表名' 分配空间,因为文件组 'PRIMARY' 已满
    获取一个目录下文件扩展名为txt或htm或html的文件的几种方法
    由于 Web 服务器上的“ISAPI 和 CGI 限制”列表设置,无法提供您请求的页面
    图解C#创建SqlServer MD5 加密函数
    SqlServer 日期转换 所有格式
    使用SoapHeader对WebService进行身份验证
    禁用文本框粘贴功能
    去除 以下文件中的行尾不一致,要将行尾标准化吗 的提示
    程序锁定windows系统以及调用其它系统对话框,如控制面板,重启系统
    yakuake shell
  • 原文地址:https://www.cnblogs.com/sunxun/p/9243852.html
Copyright © 2011-2022 走看看