pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

Android開發之TabHost選項卡及相關疑難解決方法

這篇文章主要介紹了Android開發之TabHost選項卡及相關疑難解決方法,結合實例形式較為詳細的分析了Android開發中TabHost選項卡的常見用法以及相關疑難問題解決方法,需要的朋友可以參考下

本文實例分析了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>

你會發現并沒有什么 卵用 ?。。I?。。瑂o:

百度了半天找不到問題所在,然后。。。修改下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程序設計有所幫助。

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

這篇文章主要介紹了Android TabHost選項卡標簽圖標始終不出現的解決方法,涉及Android界面布局相關屬性與狀態設置操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之Notification手機狀態欄通知用法,結合實例形式分析了Android Notification手機狀態欄通知的常見函數、功能及使用技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發實現模仿微信小窗口功能,結合實例形式分析了Android實現微信風格Dialog對話框窗口相關功能與布局操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之PopupWindow創建彈窗、對話框的方法,結合實例形式詳細分析了Android使用PopupWindow創建對話框相關操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之DatePickerDialog、TimePickerDialog時間日期對話框用法,結合實例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時間相關操作技巧,需要的朋友可以參考
這篇文章主要介紹了Android開發之ProgressDialog進度對話框用法,簡單介紹了ProgressDialog進度對話框常見函數功能,并結合實例形式分析了ProgressDialog組件創建及使用進度對話框相關操作技巧,需
主站蜘蛛池模板: 筛分机|振动筛分机|气流筛分机|筛分机厂家-新乡市大汉振动机械有限公司 | 空气净化器租赁,空气净化器出租,全国直租_奥司汀净化器租赁 | 变频器维修公司_plc维修_伺服驱动器维修_工控机维修 - 夫唯科技 变位机,焊接变位机,焊接变位器,小型变位机,小型焊接变位机-济南上弘机电设备有限公司 | 电动车头盔厂家_赠品头盔_安全帽批发_山东摩托车头盔—临沂承福头盔 | SOUNDWELL 编码器|电位器|旋转编码器|可调电位器|编码开关厂家-广东升威电子制品有限公司 | 新能源汽车电机定转子合装机 - 电机维修设备 - 睿望达 | 服务器之家 - 专注于服务器技术及软件下载分享 | 空压机商城|空气压缩机|空压机配件-压缩机网旗下商城 | 400电话_400电话申请_888元包年_400电话办理服务中心_400VIP网 | pbootcms网站模板|织梦模板|网站源码|jquery建站特效-html5模板网 | 合肥注册公司|合肥代办营业执照、2024注册公司流程 | 大鼠骨髓内皮祖细胞-小鼠神经元-无锡欣润生物科技有限公司 | 硫酸钡厂家_高光沉淀硫酸钡价格-河南钡丰化工有限公司 | 防火窗_耐火窗_防火门厂家_防火卷帘门-重庆三乐门业有限公司 | 【甲方装饰】合肥工装公司-合肥装修设计公司,专业从事安徽办公室、店面、售楼部、餐饮店、厂房装修设计服务 | 太平洋亲子网_健康育儿 品质生活| 喷涂流水线,涂装流水线,喷漆流水线-山东天意设备科技有限公司 | 不干胶标签-不干胶贴纸-不干胶标签定制-不干胶标签印刷厂-弗雷曼纸业(苏州)有限公司 | 沈阳网站建设_沈阳网站制作_沈阳网页设计-做网站就找示剑新零售 沈阳缠绕膜价格_沈阳拉伸膜厂家_沈阳缠绕膜厂家直销 | 耐破强度测试仪-纸箱破裂强度试验机-济南三泉中石单品站 | 美国PARKER齿轮泵,美国PARKER柱塞泵,美国PARKER叶片泵,美国PARKER电磁阀,美国PARKER比例阀-上海维特锐实业发展有限公司二部 | 环球周刊网| 干法制粒机_智能干法制粒机_张家港市开创机械制造有限公司 | HYDAC过滤器,HYDAC滤芯,现货ATOS油泵,ATOS比例阀-东莞市广联自动化科技有限公司 | 杭州可当科技有限公司—流量卡_随身WiFi_AI摄像头一站式解决方案 | 广州小程序开发_APP开发公司_分销商城系统定制_小跑科技 | 首页-恒温恒湿试验箱_恒温恒湿箱_高低温试验箱_高低温交变湿热试验箱_苏州正合 | 苏州西朗门业-欧盟CE|莱茵UL双认证的快速卷帘门品牌厂家 | Pos机办理_个人商户免费POS机申请-拉卡拉办理网 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 合肥白癜风医院_[治疗白癜风]哪家好_合肥北大白癜风医院 | 制样机-密封锤式破碎机-粉碎机-智能马弗炉-南昌科鑫制样 | 深圳APP开发_手机软件APP定制外包_小程序开发公司-来科信 | 小程序开发公司-小程序制作-微信小程序开发-小程序定制-咏熠软件 | 轻型地埋电缆故障测试仪,频响法绕组变形测试仪,静荷式卧式拉力试验机-扬州苏电 | 金刚网,金刚网窗纱,不锈钢网,金刚网厂家- 河北萨邦丝网制品有限公司 | 优考试_免费在线考试系统_培训考试系统_题库系统_组卷答题系统_匡优考试 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | PTFE接头|聚四氟乙烯螺丝|阀门|薄膜|消解罐|聚四氟乙烯球-嘉兴市方圆氟塑制品有限公司 | UV-1800紫外光度计-紫外可见光度计厂家-翱艺仪器(上海)有限公司 | 一体化预制泵站-一体化提升泵站-一体化泵站厂家-山东康威环保 |