欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android简单实现屏幕下方Tab菜单的方法

程序员文章站 2024-03-06 15:51:44
本文实例讲述了android简单实现屏幕下方tab菜单的方法。分享给大家供大家参考,具体如下: 看到很多热门的android程序(如:新浪微博、腾讯微博、京东商城、淘宝、...

本文实例讲述了android简单实现屏幕下方tab菜单的方法。分享给大家供大家参考,具体如下:

看到很多热门的android程序(如:新浪微博、腾讯微博、京东商城、淘宝、当当等等)使用选项卡风格作为程序界面的主框架结构,而android的选项卡控件默认是按钮在上方的。我在网上看到有多种实现方法,这里提供一种个人觉得比较简单的。由于我对android开发所知甚少,方法的优劣目前不好评价,欢迎各位提供更好的思路。

主要原理:设置 tabwidget 控件的 android:layout_alignparentbottom="true" 实现。

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<tabhost android:id="@+id/tabhost" xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <relativelayout android:orientation="vertical"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <tabwidget android:id="@android:id/tabs"
      android:layout_width="fill_parent" android:layout_height="wrap_content"
      android:layout_alignparentbottom="true" />
    <framelayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent" android:layout_height="fill_parent">
      <linearlayout android:id="@+id/tab1"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        androidrientation="vertical">
        <textview android:id="@+id/view1" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="@string/textview_1" />
      </linearlayout>
      <linearlayout android:id="@+id/tab2"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        androidrientation="vertical">
        <textview android:id="@+id/view2" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="@string/textview_2" />
      </linearlayout>
      <linearlayout android:id="@+id/tab3"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        androidrientation="vertical">
        <textview android:id="@+id/view3" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="@string/textview_3" />
      </linearlayout>
      <linearlayout android:id="@+id/tab4"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        androidrientation="vertical">
        <textview android:id="@+id/view4" android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:text="@string/textview_4" />
      </linearlayout>
    </framelayout>
  </relativelayout>
</tabhost>

zhnews.java:

package net.zhnews.android;
import android.app.activity;
import android.os.bundle;
import android.widget.tabhost;
public class zhnews extends activity {
  /** called when the activity is first created. */
  @override
  public void oncreate(bundle icicle) {
    super.oncreate(icicle);
    setcontentview(r.layout.main);
    settitle("珠海新闻网android客户端");
    tabhost tabs = (tabhost) findviewbyid(r.id.tabhost);
    tabs.setup();
    tabhost.tabspec spec = tabs.newtabspec("tab1");
    spec.setcontent(r.id.tab1);
    spec.setindicator("新闻");
    tabs.addtab(spec);
    spec = tabs.newtabspec("tab2");
    spec.setcontent(r.id.tab2);
    spec.setindicator("搜联社");
    tabs.addtab(spec);
    spec = tabs.newtabspec("tab3");
    spec.setcontent(r.id.tab3);
    spec.setindicator("影像");
    tabs.addtab(spec);
    spec = tabs.newtabspec("tab4");
    spec.setcontent(r.id.tab4);
    spec.setindicator("设置");
    tabs.addtab(spec);
    tabs.setcurrenttab(0);
  }
}

更多关于android相关内容感兴趣的读者可查看本站专题:《android多媒体操作技巧汇总(音频,视频,录音等)》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android编程之activity操作技巧总结》、《android操作sqlite数据库技巧总结》、《android操作json格式数据技巧总结》、《android数据库操作技巧总结》、《android文件操作技巧汇总》、《android编程开发之sd卡操作方法汇总》、《android资源操作技巧汇总》及《android控件用法总结

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