久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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>

你會發現并沒有什么 卵用 !!!嘔!!,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程序設計有所幫助。

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

相關文檔推薦

這篇文章主要介紹了Android TabHost選項卡標簽圖標始終不出現的解決方法,涉及Android界面布局相關屬性與狀態設置操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之Notification手機狀態欄通知用法,結合實例形式分析了Android Notification手機狀態欄通知的常見函數、功能及使用技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發實現模仿微信小窗口功能,結合實例形式分析了Android實現微信風格Dialog對話框窗口相關功能與布局操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之PopupWindow創建彈窗、對話框的方法,結合實例形式詳細分析了Android使用PopupWindow創建對話框相關操作技巧,需要的朋友可以參考下
這篇文章主要介紹了Android開發之DatePickerDialog、TimePickerDialog時間日期對話框用法,結合實例形式分析了Android使用DatePickerDialog、TimePickerDialog顯示日期時間相關操作技巧,需要的朋友可以參考
這篇文章主要介紹了Android開發之ProgressDialog進度對話框用法,簡單介紹了ProgressDialog進度對話框常見函數功能,并結合實例形式分析了ProgressDialog組件創建及使用進度對話框相關操作技巧,需
主站蜘蛛池模板: 国产精品网址 | 久久r免费视频 | 三级黄视频在线观看 | 97国产一区二区精品久久呦 | 日韩视频一区二区三区 | 一区二区精品视频 | 国产精品久久精品 | 成人黄色av网址 | 免费黄色网址视频 | 草草草久久久 | 亚洲人在线观看视频 | 日本黄色不卡视频 | 草草草影院 | 毛片免费视频 | 久久人体视频 | 免费黄色网址视频 | 日韩在线观看精品 | 日日骚av | 日韩视频在线观看中文字幕 | 免费视频久久久久 | 精品免费国产一区二区三区四区介绍 | 操射视频| 日本一区二区不卡 | 精品国产欧美一区二区三区成人 | 欧美大片一区 | 国产成人高清在线观看 | 欧美日韩中文在线 | 综合久久网 | 亚洲精品自在在线观看 | 亚洲国产精品一区二区三区 | 蜜月aⅴ国产精品 | 欧美成人精品一区二区男人看 | 国产一级网站 | 日日日操| 久久在看| 日韩免费在线视频 | 日韩成人专区 | 欧美婷婷 | 欧美三级成人理伦 | 日韩国产欧美 | 久久成人人人人精品欧 |