zoukankan      html  css  js  c++  java
  • Android之TabHost布局

    1.概念

          盛放Tab的容器就是TabHost。TabHost的实现有两种方式:

          第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

          第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

     2.案例

    1)继承TabActivity

    res/layout/activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
     <!-- 定义TabHost组件 -->
     <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">
       <!-- 定义第一个标签页的内容 -->
       <LinearLayout android:id="@+id/tab01" 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">     <!-- 定义一个TextView用于显示标签页中的内容 -->
        <TextView
          android:text="测试"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"/>   </LinearLayout>   <!-- 定义第二个标签页的内容 -->   <LinearLayout
        android:id="@+id/tab02"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">     <!--小编太懒,里面没内容-->    
      </LinearLayout>   <!-- 定义第三个标签页的内容 -->   <LinearLayout
        android:id="@+id/tab03"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">     <!--小编太懒,里面没内容-->
      </LinearLayout>

    Main.java

    public class Main extends TabActivity {
    
       @Override
       public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
     
         //调用TabActivity的getTabHost()方法获取TabHost对象
         TabHost tabHost = getTabHost();
     
         //设置使用TabHost布局
         LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true);
         //添加第一个标签页
         tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("xxx").setContent(R.id.tab01));
         //添加第二个标签页,并在其标签上添加一个图片
         tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("xxxx",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02));
         //添加第三个标签页
         tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("xxxxxxx").setContent(R.id.tab03));
       }
     }
    

      现在讲一下tab里面的参数:

        newTabSpec实例化一个分页

        setIndicator()设置标题,也就是你看到的tab显示的内容

        setContent()设置内容,可以是一个id,也可以是activity

        setContent(new Intent(this,Activity.class))

        

    2)不继承TabActivity

         继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。

    res/layout/activity_main.xml代码
    <?xml version="1.0" encoding="UTF-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/tabhost">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TabWidget
                android:id="@android:id/tabs"
                android:layout_alignParentBottom="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_above="@android:id/tabs"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <LinearLayout
                    android:id="@+id/tab01"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <SearchView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
    
                    </SearchView>
                    <ScrollView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                        <LinearLayout
                            android:orientation="vertical"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content">
    
                            <TextView
                                android:id="@+id/text03"
                                android:text="正在设计中"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content" />
                        </LinearLayout>
                    </ScrollView>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab02"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="敬请期待"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab03"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="敬请期待"/>
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/tab04"
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="敬请期待"/>
                </LinearLayout>
            </FrameLayout>
        </RelativeLayout>
    </TabHost>
    

      Main.java

    public class Main extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //设置TabHost
            setTabHost();
    
        //  设置tabhost,要设置图片内容,请参考上面
        protected void setTabHost() {
            TabHost tabHost = (TabHost) findViewById(R.id.tabhost);
            tabHost.setup();
            tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("xxx")
                    .setContent(R.id.tab01));
            tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("xxxx")
                    .setContent(R.id.tab02));
            tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("xxxxx")
                    .setContent(R.id.tab03));
            tabHost.addTab(tabHost.newTabSpec("tab4").setIndicator("xxxx")
                    .setContent(R.id.tab04));
        }
    }
    

      目前的问题及解决方法

      1、要把导航栏放下面怎么弄,比较懒,直接把一个项目里面用到了的贴上来,删除了一部分不用的

    <?xml version="1.0" encoding="UTF-8"?>
    <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/tabhost">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TabWidget
                android:id="@android:id/tabs"
    <!--看到没有,这里这里,直接设置为在最下面--> android:layout_alignParentBottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_above="@android:id/tabs" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout> <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="敬请期待"/> </LinearLayout> <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="敬请期待"/> </LinearLayout> <LinearLayout android:id="@+id/tab04" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="敬请期待"/> </LinearLayout> </FrameLayout> </RelativeLayout> </TabHost>

      2、输入法把tab顶上去了怎么办

        在AndroidManifest.xml的activity里面加一句话

    android:windowSoftInputMode="stateVisible|adjustPan"
    

      



  • 相关阅读:
    内存泄漏 Memory Leaks 内存优化 MD
    Handler Thread 内部类引起内存泄露分析
    为什么不取消注册BroadcastReceiver会导致内存泄漏
    WebChromeClient 简介 API 案例
    WebViewClient 简介 API 案例
    java.net.URI 简介 文档 API
    android.net.Uri 简介 API
    RV 多样式 MultiType 聊天界面 消息类型 MD
    JS函数声明与定义,作用域,函数声明与表达式的区别
    CSS中table tr:nth-child(even)改变tr背景颜色: IE7,8无效
  • 原文地址:https://www.cnblogs.com/wabi87547568/p/4943480.html
Copyright © 2011-2022 走看看