当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程实现在底端显示选项卡的方法

Android编程实现在底端显示选项卡的方法

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android编程实现在底端显示选项卡的方法。分享给大家供大家参考,具体如下:

1.layout 文件

<?xml version="1.0" encoding="utf-8" ?>
<linearlayout xmlns: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">
      <framelayout android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:padding="5dp" android:layout_weight="1">
        <linearlayout android:id="@+id/widget_layout_blue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">
          <relativelayout android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:paddingleft="3px"
          android:paddingright="3px">
            <linearlayout android:id="@+id/titlelayout_person_check_road_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            </linearlayout>
            <linearlayout
            android:id="@+id/layout_person_check_road_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_torightof="@id/titlelayout_person_check_road_add"
            android:layout_aligntop="@id/titlelayout_person_check_road_add"
            android:orientation="vertical">
            </linearlayout>
        </relativelayout>
    </linearlayout>
    <linearlayout android:id="@+id/widget_layout_red"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
      <relativelayout android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:paddingleft="3px" android:paddingright="3px">
        <linearlayout android:id="@+id/titlelayout_person_check_road_add1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        </linearlayout>
        <linearlayout
        android:id="@+id/layout_person_check_road_add1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_torightof="@id/titlelayout_person_check_road_add1"
        android:layout_aligntop="@id/titlelayout_person_check_road_add1"
        android:orientation="vertical">
        </linearlayout>
    </relativelayout>
</linearlayout>
<linearlayout android:id="@+id/widget_layout_green"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
  <relativelayout android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingleft="3px" android:paddingright="3px">
    <linearlayout android:id="@+id/titlelayout_person_check_road_add2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"> </linearlayout>
    <linearlayout android:id="@+id/layout_person_check_road_add2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_torightof="@id/titlelayout_person_check_road_add2"
    android:layout_aligntop="@id/titlelayout_person_check_road_add2"
    android:orientation="vertical"> </linearlayout>
  </relativelayout>
</linearlayout>
<linearlayout android:id="@+id/widget_layout_yellow"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
  <relativelayout android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:paddingleft="3px" android:paddingright="3px">
    <linearlayout android:id="@+id/titlelayout_person_check_road_add3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"> </linearlayout>
    <linearlayout android:id="@+id/layout_person_check_road_add3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_torightof="@id/titlelayout_person_check_road_add3"
    android:layout_aligntop="@id/titlelayout_person_check_road_add3"
    android:orientation="vertical"> </linearlayout>
  </relativelayout>
</linearlayout>
</framelayout>
<tabwidget android:id="@android:id/tabs"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_weight="0" /> </linearlayout>
</tabhost>
</linearlayout>

2.java 文件

import android.app.activitygroup;
import android.graphics.color;
import android.os.bundle;
import android.widget.edittext;
import android.widget.linearlayout;
import android.widget.linearlayout.layoutparams;
import android.widget.tabhost;
import android.widget.textview;
public class tabbottom extends activitygroup {
  public static tabhost mytabhost;
  private layoutparams title_params = new layoutparams(120, 50);
  private layoutparams content_params = new layoutparams(158, 50);
  private linearlayout titlelayout, showviewlayout, titlelayout1,
  showviewlayout1, titlelayout2, showviewlayout2, titlelayout3,
  showviewlayout3;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.bottomtab);
    mytabhost = (tabhost) findviewbyid(r.id.edit_item_tab_host);
    mytabhost.setup(this.getlocalactivitymanager());
    titlelayout = (linearlayout) findviewbyid(r.id.titlelayout_person_check_road_add);
    showviewlayout = (linearlayout) findviewbyid(r.id.layout_person_check_road_add);
    titlelayout1 = (linearlayout) findviewbyid(r.id.titlelayout_person_check_road_add1);
    showviewlayout1 = (linearlayout) findviewbyid(r.id.layout_person_check_road_add1);
    titlelayout2 = (linearlayout) findviewbyid(r.id.titlelayout_person_check_road_add2);
    showviewlayout2 = (linearlayout) findviewbyid(r.id.layout_person_check_road_add2);
    titlelayout3 = (linearlayout) findviewbyid(r.id.titlelayout_person_check_road_add3);
    showviewlayout3 = (linearlayout) findviewbyid(r.id.layout_person_check_road_add3);
    mytabhost.addtab(mytabhost
    .newtabspec("one")
    .setindicator("收件箱",
    getresources().getdrawable(r.drawable.icon))
    .setcontent(r.id.widget_layout_blue));
    mytabhost.addtab(mytabhost
    .newtabspec("two")
    .setindicator("发件箱",
    getresources().getdrawable(r.drawable.icon))
    .setcontent(r.id.widget_layout_green));
    mytabhost.addtab(mytabhost
    .newtabspec("three")
    .setindicator("垃圾箱",
    getresources().getdrawable(r.drawable.icon))
    .setcontent(r.id.widget_layout_red));
    mytabhost.addtab(mytabhost
    .newtabspec("four")
    .setindicator("发送",
    getresources().getdrawable(r.drawable.icon))
    .setcontent(r.id.widget_layout_yellow));
    showinlayout();
    showinlayout1();
    showinlayout2();
    showinlayout3();
  }
  /*
  * 填充第一个选项卡页面
  */
  private void showinlayout() {
    for (int i = 0; i < 10; i++) {
      string condname = "名称显示1";
      textview tv = new textview(this);
      tv.settextcolor(color.white);
      tv.settextsize(16);
      tv.setlayoutparams(title_params);
      tv.settext(condname + ":");
      titlelayout.addview(tv);
      edittext tv1 = new edittext(this);
      tv1.settextsize(16);
      tv1.setlayoutparams(content_params);
      tv1.settext(condname);
      showviewlayout.addview(tv1);
    }
  }
  private void showinlayout1() {
    for (int i = 0; i < 10; i++) {
      string condname = "名称显示2";
      textview tv = new textview(this);
      tv.settextcolor(color.white);
      tv.settextsize(16);
      tv.setlayoutparams(title_params);
      tv.settext(condname + ":");
      titlelayout1.addview(tv);
      textview tv1 = new textview(this);
      tv1.settextcolor(color.white);
      tv1.settextsize(16);
      tv1.setlayoutparams(content_params);
      tv1.settext(condname + ":");
      showviewlayout1.addview(tv1);
    }
  }
  private void showinlayout2() {
    for (int i = 0; i < 10; i++) {
      string condname = "名称显示3";
      textview tv = new textview(this);
      tv.settextcolor(color.white);
      tv.settextsize(16);
      tv.setlayoutparams(title_params);
      tv.settext(condname + ":");
      titlelayout2.addview(tv);
      textview tv1 = new textview(this);
      tv1.settextcolor(color.white);
      tv1.settextsize(16);
      tv1.setlayoutparams(content_params);
      tv1.settext(condname + ":");
      showviewlayout2.addview(tv1);
    }
  }
  private void showinlayout3() {
    for (int i = 0; i < 10; i++) {
      string condname = "名称显示4";
      textview tv = new textview(this);
      tv.settextcolor(color.white);
      tv.settextsize(16);
      tv.setlayoutparams(title_params);
      tv.settext(condname + ":");
      titlelayout3.addview(tv);
      textview tv1 = new textview(this);
      tv1.settextcolor(color.white);
      tv1.settextsize(16);
      tv1.setlayoutparams(content_params);
      tv1.settext(condname + ":");
      showviewlayout3.addview(tv1);
    }
  }
}

效果如下:

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

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网