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"

  



posted @ 2015-11-06 19:52  dreamOwn  阅读(956)  评论(0编辑  收藏  举报