当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程实现将tab选项卡放在屏幕底部的方法

Android编程实现将tab选项卡放在屏幕底部的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论
本文实例讲述了android编程实现将tab选项卡放在屏幕底部的方法。分享给大家供大家参考,具体如下: 今天写tab的时候由于tab的跳转问题去查资料,倒反而发现更有趣的

本文实例讲述了android编程实现将tab选项卡放在屏幕底部的方法。分享给大家供大家参考,具体如下:

今天写tab的时候由于tab的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将tab放置在屏幕的底端。有点类似iphone里的布局了,呵呵~(其实后来发现这个应该不是用tab做的,而是buttonbar做出来的吧,或者是他重写了tab?总之不是简单地将tab放置底端了)。

要放置底端,那么android自带的例程是不可以做到的(例程参看development-apidemo)。先需要写一个xml的layout文档,命名为bottomtab.xml。

<?xml version="1.0"encoding="utf-8"?>
<linearlayoutxmlns:androidlinearlayoutxmlns: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/edit_item_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">
      <framelayoutandroid:idframelayoutandroid: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>

然后以下就是完整的代码了:

import android.app.activitygroup;
import android.content.intent;
import android.os.bundle;
import android.widget.tabhost;
import android.widget.tabhost.tabspec;
public class testtab extends activitygroup {
  publicstatic tabhost tab_host;
  @override
  protectedvoid oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.bottomtab);
    tab_host = (tabhost) findviewbyid(r.id.edit_item_tab_host);
    tab_host.setup(this.getlocalactivitymanager());
    tabspec ts1 = tab_host.newtabspec("tab_weather");
    ts1.setindicator("weather");
    ts1.setcontent(new intent(this, weather.class));
    tab_host.addtab(ts1);
    tabspec ts2 = tab_host.newtabspec("tab_mail");
    ts2.setindicator("mail");
    ts2.setcontent(new intent(this, mailsend.class));
    tab_host.addtab(ts2);
    tabspec ts3 = tab_host.newtabspec("tab_jump");
    ts3.setindicator("jump");
    ts3.setcontent(new intent(this, tabjump.class));
    tab_host.addtab(ts3);
    tab_host.setcurrenttab(0);
  }
}

而关于页面的跳转,就是:

testtab.tabhost.setcurrenttab(0);

如此这般,就形成了下面的这个东西,其实还没有放在上面好看。。。所以也证实了上面那个应用不是简单地放置tab在底端了。有机会还是再看看buttonbar了。

注意:weather.class 等activity要在 在androidmenifest.xml添加声明

在 application tag之间加上如下代码:

</activity>
   <activityandroid:nameactivityandroid:name=".weather">
</activity>
   <activityandroid:nameactivityandroid:name=".mailsend">
</activity>
   <activityandroid:nameactivityandroid:name=".tabjump">
</activity>

ps:关于androidmanifest.xml文件相关属性功能可参考本站在线工具:

android manifest功能与权限描述大全:

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网