当前位置: 移动技术网 > IT编程>移动开发>Android > Android开发之TabHost选项卡及相关疑难解决方法

Android开发之TabHost选项卡及相关疑难解决方法

2019年07月23日  | 移动技术网IT编程  | 我要评论

你爱我像谁伴奏,触手便当,1920x1080壁纸

本文实例分析了android开发之tabhost选项卡及相关疑难解决方法。分享给大家供大家参考,具体如下:

前言:

虽然现在谷歌已经不推荐使用tabhost,但是初学者还是很有必要接触下这一成金的经典的,本文将介绍纤细介绍这一空间的使用,以及大家可能遇到的问题。注:文末给出完整实现代码

三个问题:

1. 无法显示tabhost

2. 添加图片 + 文字 无法同时

3. 说在最后:点击事件

4. 底部导航无法实现

现在

从问题出发:

问题一:无法显示 tabhost

很多人调用tabhost的方法是:

setcontentview(r.layout.activity_main);
tabhost = gettabhost();

然后发现啥也没有,一脸蒙圈。。。 在这里建议大家采用遮掩的调用方法:

layoutinflater.from(this).inflate(r.layout.activity_main,
    tabhost.gettabcontentview(), true);

成功后的页面:

注:ui 略丑请忽视

问题二:图片、文字无法同时添加

好了,很多人辛辛苦苦把界面搞出来了,可能想搞个底部菜单 加个图片,结果凉凉 半天搞不出来 ,这里介绍一个方法 ,由于tabhost本身图片、文字冲突 ,无法添加,这是我们就得把目光迁移到自定义view上:本段参考自:

首先在/layout下建立自定义view名为:tab_indicator.xml文件

<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="0dip"
  android:layout_height="64dip"
  android:layout_weight="1"
  android:orientation="vertical"
  android:background="#45c0c0c0"
  android:padding="5dp">
  <imageview android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerhorizontal="true"
    />
  <textview android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignparentbottom="true"
    android:layout_centerhorizontal="true"
    style="?android:attr/tabwidgetstyle"
    />
</relativelayout>

接着,紧随其后在/drawable下添加:tab_info.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/find"
    android:state_selected="true" />
  <item android:drawable="@drawable/find1" />
</selector>

这些都搞定之后,就可以在活动中调用了:

首先在活动中先建立addtab()方法:

private void addtab(string label, int drawableid) {
  intent intent = new intent(this, textactivity.class);
  tabhost.tabspec spec = tabhost.newtabspec(label);
  view tabindicator = layoutinflater.from(this).inflate(r.layout.tab_indicator, gettabwidget(), false);
  textview title = (textview) tabindicator.findviewbyid(r.id.title);
  title.settext(label);
  imageview icon = (imageview) tabindicator.findviewbyid(r.id.icon);
  icon.setimageresource(drawableid);
  spec.setindicator(tabindicator);
  spec.setcontent(intent);
  tabhost.addtab(spec);
}

终于我们。。。:

成功了!!!

问题三:添加监听事件

这个无脑 只要 id 匹配就行了,直接上代码:

tabhost.setontabchangedlistener(new tabhost.ontabchangelistener(){
  @override
  // tabid是newtabspec参数设置的tab页名,并不是layout里面的标识符id
  public void ontabchanged(string tabid) {
    if (tabid.equals("tab1")) {  //第一个标签
      toast.maketext(mainactivity.this, "点击标签页一", toast.length_short).show();
    }else if (tabid.equals("tab2")) {  //第二个标签
      toast.maketext(mainactivity.this, "点击标签页二", toast.length_short).show();
    }else if (tabid.equals("tab3")) {  //第三个标签
      toast.maketext(mainactivity.this, "点击标签页三", toast.length_short).show();
    }
  }
});

暂时能记起来的 疑难就这些了 如果还有请给我留言 我尽力解答。。

附上布局与实现:

布局:

<?xml version="1.0" encoding="utf-8" ?>
<tabhost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@android:id/tabhost"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_weight="1"
  android:scrollbarsize="100dp">
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <tabwidget
      android:id="@android:id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content">
      <framelayout
        android:id="@android:id/tabcontent"
        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">
          <textview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text11"
            android:textsize="20dp"/>
          <textview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text12"
            android:textsize="20dp"/>
        </linearlayout>
        <!--定义第二个标签页的内容-->
        <linearlayout
          android:id="@+id/tab02"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <textview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text11"
            android:textsize="20dp"/>
          <textview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text12"
            android:textsize="20dp"/>
        </linearlayout>
        <!--定义第三个标签页的内容-->
        <linearlayout
          android:id="@+id/tab03"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
          <textview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text11"
            android:textsize="20dp"/>
          <textview
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="text12"
            android:textsize="20dp"/>
        </linearlayout>
      </framelayout>
    </tabwidget>
  </linearlayout>
</tabhost>

实现:

public class mainactivity extends tabactivity {
  tabhost tabhost;
  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
//    setcontentview(r.layout.activity_main);
    tabhost = gettabhost();
    layoutinflater.from(this).inflate(r.layout.activity_main,
        tabhost.gettabcontentview(), true);
    addtab("tab1", r.drawable.tab_info);
    addtab("tab2", r.drawable.tab_info);
    addtab("tab3", r.drawable.tab_info);
//
    //标签切换事件处理,setontabchangedlistener
    tabhost.setontabchangedlistener(new tabhost.ontabchangelistener(){
      @override
      // tabid是newtabspec参数设置的tab页名,并不是layout里面的标识符id
      public void ontabchanged(string tabid) {
        if (tabid.equals("tab1")) {  //第一个标签
          toast.maketext(mainactivity.this, "点击标签页一", toast.length_short).show();
        }else if (tabid.equals("tab2")) {  //第二个标签
          toast.maketext(mainactivity.this, "点击标签页二", toast.length_short).show();
        }else if (tabid.equals("tab3")) {  //第三个标签
          toast.maketext(mainactivity.this, "点击标签页三", toast.length_short).show();
        }
      }
    });
  }
  private void addtab(string label, int drawableid) {
    intent intent = new intent(this, textactivity.class);
    tabhost.tabspec spec = tabhost.newtabspec(label);
    view tabindicator = layoutinflater.from(this).inflate(r.layout.tab_indicator, gettabwidget(), false);
    textview title = (textview) tabindicator.findviewbyid(r.id.title);
    title.settext(label);
    imageview icon = (imageview) tabindicator.findviewbyid(r.id.icon);
    icon.setimageresource(drawableid);
    spec.setindicator(tabindicator);
    spec.setcontent(intent);
    tabhost.addtab(spec);
  }
}

ps:新建的layout和/drawable里的xml文件在问题给过,这里就不反复给了。

问题四:底部导航效果无法实现

底部导航的参见方法是把tabwidget放在framelayout后面,但是啧啧。。。

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent" >
  <tabhost
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignparentbottom="true"
    android:layout_alignparentleft="true" >
    <linearlayout
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" >
      <framelayout
        android:id="@android:id/tabcontent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="top">
        中间内容前面给出 这里省略
      </framelayout>
    </linearlayout>
    <tabwidget
      android:id="@android:id/tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom" >
    </tabwidget>
  </tabhost>
</relativelayout>

你会发现并没有什么 卵用 !!!呕!!,so:

百度了半天找不到问题所在,然后。。。修改下mainactivity

@override
protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  //原来
//    tabhost = gettabhost();
//    layoutinflater.from(this).inflate(r.layout.activity_main,
//        tabhost.gettabcontentview(), true);
  //修改后
  setcontentview(r.layout.activity_main);
  tabhost = gettabhost();
  tabhost.setup(this.getlocalactivitymanager());
  addtab("tab1", r.drawable.tab_info);
  addtab("tab2", r.drawable.tab_info);
  addtab("tab3", r.drawable.tab_info);
  //标签切换事件处理,setontabchangedlistener
  iniclick();
}

注:此处我已经将点击事件封装到方法中

最后:全剧终

哦,还没有且等我放下最后的图。。

啧啧,搞定

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

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网