最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法

 更新時間:2019年03月16日 10:24:41   作者:水中魚之1999  
這篇文章主要介紹了Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android開發(fā)中TabHost選項(xiàng)卡的常見用法以及相關(guān)疑難問題解決方法,需要的朋友可以參考下

本文實(shí)例分析了Android開發(fā)之TabHost選項(xiàng)卡及相關(guān)疑難解決方法。分享給大家供大家參考,具體如下:

前言:

雖然現(xiàn)在谷歌已經(jīng)不推薦使用TabHost,但是初學(xué)者還是很有必要接觸下這一成金的經(jīng)典的,本文將介紹纖細(xì)介紹這一空間的使用,以及大家可能遇到的問題。注:文末給出完整實(shí)現(xiàn)代碼

三個問題:

1. 無法顯示TabHost

2. 添加圖片 + 文字 無法同時

3. 說在最后:點(diǎn)擊事件

4. 底部導(dǎo)航無法實(shí)現(xiàn)

現(xiàn)在

從問題出發(fā):

問題一:無法顯示 TabHost

很多人調(diào)用TabHost的方法是:

setContentView(R.layout.activity_main);
tabHost = getTabHost();

然后發(fā)現(xiàn)啥也沒有,一臉蒙圈。。。 在這里建議大家采用遮掩的調(diào)用方法:

LayoutInflater.from(this).inflate(R.layout.activity_main,
    tabHost.getTabContentView(), true);

成功后的頁面:

注:UI 略丑請忽視

問題二:圖片、文字無法同時添加

好了,很多人辛辛苦苦把界面搞出來了,可能想搞個底部菜單 加個圖片,結(jié)果涼涼 半天搞不出來 ,這里介紹一個方法 ,由于TabHost本身圖片、文字沖突 ,無法添加,這是我們就得把目光遷移到自定義view上:本段參考自:http://m.fzitv.net/article/157914.htm

首先在/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>

這些都搞定之后,就可以在活動中調(diào)用了:

首先在活動中先建立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);
}

終于我們。。。:

成功了?。?!

問題三:添加監(jiān)聽事件

這個無腦 只要 id 匹配就行了,直接上代碼:

tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
  @Override
  // tabId是newTabSpec參數(shù)設(shè)置的tab頁名,并不是layout里面的標(biāo)識符id
  public void onTabChanged(String tabId) {
    if (tabId.equals("tab1")) {  //第一個標(biāo)簽
      Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁一", Toast.LENGTH_SHORT).show();
    }else if (tabId.equals("tab2")) {  //第二個標(biāo)簽
      Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁二", Toast.LENGTH_SHORT).show();
    }else if (tabId.equals("tab3")) {  //第三個標(biāo)簽
      Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁三", Toast.LENGTH_SHORT).show();
    }
  }
});

暫時能記起來的 疑難就這些了 如果還有請給我留言 我盡力解答。。

附上布局與實(shí)現(xiàn):

布局:

<?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">
        <!--定義第一個標(biāo)簽頁特內(nèi)容-->
        <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>
        <!--定義第二個標(biāo)簽頁的內(nèi)容-->
        <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>
        <!--定義第三個標(biāo)簽頁的內(nèi)容-->
        <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>

實(shí)現(xiàn):

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);
//
    //標(biāo)簽切換事件處理,setOnTabChangedListener
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener(){
      @Override
      // tabId是newTabSpec參數(shù)設(shè)置的tab頁名,并不是layout里面的標(biāo)識符id
      public void onTabChanged(String tabId) {
        if (tabId.equals("tab1")) {  //第一個標(biāo)簽
          Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁一", Toast.LENGTH_SHORT).show();
        }else if (tabId.equals("tab2")) {  //第二個標(biāo)簽
          Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁二", Toast.LENGTH_SHORT).show();
        }else if (tabId.equals("tab3")) {  //第三個標(biāo)簽
          Toast.makeText(MainActivity.this, "點(diǎn)擊標(biāo)簽頁三", 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文件在問題給過,這里就不反復(fù)給了。

問題四:底部導(dǎo)航效果無法實(shí)現(xiàn)

底部導(dǎo)航的參見方法是把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">
        中間內(nèi)容前面給出 這里省略
      </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>

你會發(fā)現(xiàn)并沒有什么 卵用 ?。。I??!,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);
  //標(biāo)簽切換事件處理,setOnTabChangedListener
  iniClick();
}

注:此處我已經(jīng)將點(diǎn)擊事件封裝到方法中

最后:全劇終

哦,還沒有且等我放下最后的圖。。

嘖嘖,搞定

更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)

希望本文所述對大家Android程序設(shè)計有所幫助。

相關(guān)文章

  • Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽

    Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽

    這篇文章主要介紹了Android App的運(yùn)行環(huán)境及Android系統(tǒng)架構(gòu)概覽,并對應(yīng)用程序進(jìn)程間隔離機(jī)制等知識點(diǎn)作了介紹,需要的朋友可以參考下
    2016-03-03
  • Android高級動畫篇之SVG矢量動畫范例

    Android高級動畫篇之SVG矢量動畫范例

    矢量動畫即是在計算機(jī)中使用數(shù)學(xué)方程來描述屏幕上復(fù)雜的曲線,利用圖形的抽象運(yùn)動特征來記錄變化的畫面信息的動畫,本篇帶你了解在Android中的矢量動畫
    2021-11-11
  • Android項(xiàng)目中使用Eclipse導(dǎo)出jar文件的操作方法

    Android項(xiàng)目中使用Eclipse導(dǎo)出jar文件的操作方法

    文章講述了如何使用Eclipse將Android項(xiàng)目打包成jar文件,并詳細(xì)解答了在打包過程中遇到的問題,如如何處理依賴的jar或library,以及如何解決在其他項(xiàng)目中引用時出現(xiàn)的NoClassDefFoundError錯誤
    2025-02-02
  • Android CameraManager類詳解

    Android CameraManager類詳解

    這篇文章主要為大家詳細(xì)介紹了Android CameraManager類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • Android實(shí)現(xiàn)圓圈倒計時

    Android實(shí)現(xiàn)圓圈倒計時

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓圈倒計時,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • Flutter渲染原理深入解析

    Flutter渲染原理深入解析

    眾所周知?Flutter是由Google推出的開源的高性能跨平臺框架,一個2D渲染引擎。在Flutter中,Widget是Flutter用戶界面的基本構(gòu)成單元,可以說一切皆Widget。下面來看下Flutter框架的整體結(jié)構(gòu)組成
    2023-04-04
  • Android控件Tween動畫(補(bǔ)間動畫)實(shí)現(xiàn)方法示例

    Android控件Tween動畫(補(bǔ)間動畫)實(shí)現(xiàn)方法示例

    這篇文章主要介紹了Android控件Tween動畫(補(bǔ)間動畫)實(shí)現(xiàn)方法,結(jié)合具體實(shí)例形式分析了Android補(bǔ)間動畫的原理、功能實(shí)現(xiàn)與布局相關(guān)操作技巧,需要的朋友可以參考下
    2017-08-08
  • Android ViewFlipper翻轉(zhuǎn)視圖使用詳解

    Android ViewFlipper翻轉(zhuǎn)視圖使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android ViewFlipper翻轉(zhuǎn)視圖的使用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 使用User Agent分辨出Android設(shè)備類型的安全做法

    使用User Agent分辨出Android設(shè)備類型的安全做法

    這篇文章主要介紹了使用User Agent分辨出Android設(shè)備類型的安全做法,本文得出的結(jié)論是當(dāng)你依據(jù)檢測UA來判斷Android手機(jī)設(shè)備,請同時檢查android和mobile兩個字符串,需要的朋友可以參考下
    2015-01-01
  • Android convinientbanner頂部廣告輪播控件使用詳解

    Android convinientbanner頂部廣告輪播控件使用詳解

    這篇文章主要為大家詳細(xì)介紹了Android convinientbanner頂部廣告輪播控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-01-01

最新評論

三明市| 柳河县| 陈巴尔虎旗| 淮北市| 故城县| 中卫市| 巍山| 陕西省| 固原市| 海门市| 临夏市| 罗平县| 健康| 河曲县| 宜兰县| 巴塘县| 沐川县| 邵阳市| 大邑县| 炎陵县| 彩票| 屯昌县| 峨眉山市| 黄骅市| 灵宝市| 晴隆县| 临颍县| 高碑店市| 尼木县| 蛟河市| 尼勒克县| 西和县| 佳木斯市| 乌兰浩特市| 山丹县| 淳安县| 封开县| 苗栗县| 永福县| 衢州市| 资中县|