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

Android編程之TabWidget選項卡用法實例分析

 更新時間:2015年12月22日 15:51:55   作者:cappuccinoLau  
這篇文章主要介紹了Android編程之TabWidget選項卡用法,結合實例形式較為詳細的分析了TabWidget選項卡的具體實現技巧與使用注意事項,需要的朋友可以參考下

本文實例講述了Android編程之TabWidget選項卡用法。分享給大家供大家參考,具體如下:

1 概覽

TabWidget與TabHost。tab組件一般包括TabHost和TabWidget、FrameLayout,且TabWidget、FrameLayout屬于TabHost。

是否繼承TabActivity的問題

實現步驟。兩種實現方式,一種是將每個Tab的布局嵌在TabHost中的FrameLayout中,每個Tab的內容布局與顯示都在FrameLayout中進行,缺點是布局會顯得很臃腫;一種是每個Tab從FrameLayout中獨立出來,以Activity呈現,這樣使得每個Tab有單獨的布局。

2 效果圖

Widget在頂部的情形:

3 主要布局

3.1 TabMain布局

方式一:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/tabhost"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" >
  <RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TabWidget
      android:id="@android:id/tabs"
      android:layout_width="fill_parent"
      android:layout_height="60dip"
      android:layout_alignParentBottom="true"
      android:background="#424242" >
    </TabWidget>
    <FrameLayout
      android:id="@android:id/tabcontent"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent" >
      <LinearLayout
        android:id="@+id/theme"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
          android:id="@+id/theme_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Tab1" />
      </LinearLayout>
      <LinearLayout
        android:id="@+id/wallpaper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
          android:id="@+id/wallpaper_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Tab2" />
      </LinearLayout>
      <LinearLayout
        android:id="@+id/iconbg"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
          android:id="@+id/iconbg_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Tab3" />
      </LinearLayout>
      <LinearLayout
        android:id="@+id/screenlock"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
          android:id="@+id/screenlock_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Tab4" />
      </LinearLayout>
      <LinearLayout
        android:id="@+id/effect"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >
        <TextView
          android:id="@+id/effect_title"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Tab5" />
      </LinearLayout>
    </FrameLayout>
  </RelativeLayout>
</TabHost>

方式二:

<?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="fill_parent"
   android:layout_height="fill_parent"
   android:background="@color/wcity_normal_bg" >
  <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
  <FrameLayout
     android:id="@android:id/tabcontent"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:layout_weight="1" >
  </FrameLayout>
  <TabWidget
     android:id="@android:id/tabs"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:background="@drawable/tab"
     />
   </LinearLayout>
</TabHost>

3.2 TabItem布局

這一部分中方式一與方式二沒有什么區(qū)別,只有表示形式的區(qū)別。比如,根據需求,Tab可以

只以文字呈現,

可以只以圖片呈現,

可以同時有圖片和文字

其中有文字和圖片的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:gravity="center_horizontal|center_vertical"
  android:orientation="vertical" >
  <LinearLayout
    android:id="@+id/tabItem
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/bg_ispressed"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >
    <ImageView
      android:id="@+id/icon"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
    <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content" />
  </LinearLayout>
</LinearLayout>

3.3點擊狀態(tài)

Tab鍵點擊后狀態(tài)的問題,如果點擊后,沒有狀態(tài)提示對用戶是不友好的。點擊狀態(tài)的實現就是對TabItem布局的android:background進行設置。例如:

上述TabItem中LinearLayout的android:background設置的屬性:@drawable/bg_ispressed

其中bg_ispressed文件如下:

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

tab_selected_bg即是點擊后變換的圖片效果。

3.4 關于Tab位置的問題

Tab標簽顯示在頂部還是底部也是經常會遇到的問題。

通常TabMain布局中TabWidget在FrameLayout上面默認就是顯示在頂部了,如果改成在底部顯示,首先會想到的是直接調換順序,將TabWidget放在FrameLayout后面。

情形一:

問題來了,Tab欄直接消失掉(我試過),后來解決方法是:FrameLayout中添加屬性:android:layout_weight="1"。這種情形可以解決的條件是,TabWidget和FrameLayout被嵌套在LinearLayout布局中,如果是其他則行不通。

情形二:

TabWidget與FrameLayout順序任意,在TabWidget中添加屬性

復制代碼 代碼如下:
android:layout_alignParentBottom="true"

當然,這種情形適合TabWidget和FrameLayout被嵌套在RelativeLayout布局中,同樣的,如果是其他則行不通。

注:以上兩種情形也不是絕對的,只實踐過以上兩種情形,至于其他布局就不清楚了,具體問題具體分析吧。

4 繼承TabActivity?

4.1 繼承TabActivity與不繼承的問題

繼承不繼承TabActivity,看自己習慣了,都能正確實現,沒什么區(qū)別,至于在代碼方面唯一的區(qū)別在于:

不繼承TabActivity而繼承Activity的需要在代碼中加入:

復制代碼 代碼如下:
mTabHost.setup();

4.2 主要代碼

直接繼承自Activity的代碼

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
public class TabDesignActivity extends Activity{
  private Context mContex = this;
  private TabHost mTabHost;
  private String TAB1 = "tab1";
  private String TAB2 = "tab2";
  private String TAB3 = "tab3";
  private String TAB4 = "tab4";
  private String TAB5 = "tab5";
  private List<LinearLayout> menuItemList;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_main);
    menuItemList = new ArrayList<LinearLayout>();
    mTabHost = (TabHost) findViewById(R.id.tabhost);
    mTabHost.setup();
    mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(getMenuItem(R.drawable.tab1_ispressed, TAB1)).setContent(R.id.tab1));
    mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(getMenuItem(R.drawable.tab2_ispressed, TAB2)).setContent(R.id.tab2));
    mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(getMenuItem(R.drawable.tab3_ispressed, TAB3)).setContent(R.id.tab3));
    mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator(getMenuItem(R.drawable.tab4_ispressed, TAB4)).setContent(R.id.tab4));
    mTabHost.addTab(mTabHost.newTabSpec("tab5").setIndicator(getMenuItem(R.drawable.tab5_ispressed, TAB5)).setContent(R.id.tab5));
  }
  public View getMenuItem(int imgID, String textID){
    LinearLayout ll = (LinearLayout)LayoutInflater.from(mContex).inflate(R.layout.tab_item, null);
    ImageView imgView = (ImageView)ll.findViewById(R.id.icon);
    imgView.setBackgroundResource(imgID);
    TextView textView = (TextView)ll.findViewById(R.id.name);
    textView.setText(textID);
    menuItemList.add(ll);
    return ll;
  }
}

繼承自TabActivity的實現

/**
 * @author aaron
 */
package com.aaron.activity;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.TabHost.TabSpec;
import com.aaron.util.R;
/**
 * @author aaron
 * 
 */
public class TabWidget extends TabActivity {// 聲明TabHost對象
  private TabHost mTabhost;
  private LayoutInflater mInflater;
  private List<TextView> mtext;
  private List<ImageView> mimage;
  private List<TabSpec> mTabSpec;
  private List<LinearLayout> linearLayout;
  private List<Intent> intent;
  private Context mContext;
  private static final String[] tabTitle = { "Tab1", "Tab2", "Tab3", "Tab4","Tab5"};
  private static final int[] tabImage = { R.drawable.main1, R.drawable.main2, R.drawable.main3, R.drawable.main4,R.drawable.main5};
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab_main);
    mContext = this;
    mInflater = LayoutInflater.from(this);
    mTabhost = (TabHost) findViewById(android.R.id.tabhost);
    mTabSpec = new ArrayList<TabSpec>();
    linearLayout = new ArrayList<LinearLayout>();
    mtext = new ArrayList<TextView>();
    intent = new ArrayList<Intent>();
    //****************************************************************
    //若是引用有圖片的布局
    mimage = new ArrayList<ImageView>();
    //****************************************************************
    creatTab();
  }
  @SuppressLint("NewApi")
  public void creatTab() {
    for (int i = 0; i < tabTitle.length; i++) {
      mTabSpec.add(mTabhost.newTabSpec(tabTitle[i]));
      //****************************************************************
      //選擇使用哪種布局
      //****************************************************************
      linearLayout.add((LinearLayout) mInflater.inflate(R.layout.tabwidget2, null)); 
      mtext.add((TextView) linearLayout.get(i).findViewById(R.id.tab_Text_name));
      mtext.get(i).setText(tabTitle[i]);
      //****************************************************************
      //若是引用有圖片的布局依次添加進圖片
      mimage.add((ImageView) linearLayout.get(i).findViewById(R.id.tab_Image_name));
      mimage.get(i).setImageResource(tabImage[i]);
      //****************************************************************
      // 依次加入每個Tab的Activity
      switch (i) {
      case 0:
        intent.add(new Intent().setClass(TabWidget.this, UdoActivity.class));
        break;
      case 1:
        intent.add(new Intent().setClass(TabWidget.this, UdoActivity.class));
        break;
      case 2:
        intent.add(new Intent().setClass(TabWidget.this, UdoActivity.class));
        break;
      case 3:
        intent.add(new Intent().setClass(TabWidget.this, UdoActivity.class));
        break;
      case 4:
        intent.add(new Intent().setClass(TabWidget.this, UdoActivity.class));
        break;
      }
      mTabSpec.get(i).setIndicator(linearLayout.get(i));
      mTabSpec.get(i).setContent(intent.get(i));
      mTabhost.addTab(mTabSpec.get(i));
    }
}

4.3 關鍵代碼詳解

1)mTabHost.newTabSpec("tab1")用來new一個tab,同時標記這個tab的tag。
2)setContent()用來處理點擊這個tab后的動作,可以是這個Activity下的一個組件,如setContent(R.id.tab1),也可以是一個intent,比如:setContent(newIntent(this, SubTab.class))。
3)setIndicator()用來標記這個tab的名字,可以是setIndicator("tab1"),也可以包含其他的屬性,如圖片:setIndicator( "名稱",getResources().getDrawable(android.R.tab1))。
4)tabs.addTab(spec)將這個tab添加進TabHost。
5)getMenuItem(R.drawable.tab_ispressed,TAB1)設置其中一Tab被按下的狀態(tài)改變,R.drawable.tab_ispressed布局如下:

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

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

相關文章

  • Android編程實現自動調整TextView字體大小以適應文字長度的方法

    Android編程實現自動調整TextView字體大小以適應文字長度的方法

    這篇文章主要介紹了Android編程實現自動調整TextView字體大小以適應文字長度的方法,涉及Android基于TextView類的繼承及Paint屬性操作實現字體大小自適應的相關技巧,需要的朋友可以參考下
    2016-01-01
  • Android?Jetpack組件Navigation導航組件的基本使用

    Android?Jetpack組件Navigation導航組件的基本使用

    本篇主要簡單介紹了一下?Navigation?是什么?以及使用它的流程是什么,并且結合實際案例?操作了一番,Navigation?還有很多其他用法,如條件導航、嵌套圖、過度動畫?等等功能?有機會再操作,需要的朋友可以參考下
    2022-06-06
  • Android中Fragment多層嵌套時onActivityResult無法正確回調問題的解決方法

    Android中Fragment多層嵌套時onActivityResult無法正確回調問題的解決方法

    這篇文章主要介紹了Android中Fragment多層嵌套時onActivityResult無法正確回調問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android中Intent與Bundle的使用詳解

    Android中Intent與Bundle的使用詳解

    這篇文章主要給大家總結介紹了關于Android中傳值Intent與Bundle的關系,文中通過示例代碼以及圖文介紹的非常詳細,對各位Android開發(fā)者們具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧<BR>
    2022-11-11
  • Android中使用OkHttp包處理HTTP的get和post請求的方法

    Android中使用OkHttp包處理HTTP的get和post請求的方法

    OkHttp包為安卓開發(fā)中的HTTP協議網絡編程帶來了很大的便利,這里我們就來看一下最基本的、Android中使用OkHttp包處理HTTP的get和post請求的方法:
    2016-07-07
  • Android Notification通知解析

    Android Notification通知解析

    這篇文章主要針對Android Notification通知進行解析,本文主要介紹的是notification通知的使用方法,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Android路由框架ARouter的使用示例

    Android路由框架ARouter的使用示例

    組件化或者模塊化開發(fā)模式,已逐漸成為熱浪的形式,使用這些模式可以讓我們程序更容易的擴展、更方便的維護、更快捷的同步開發(fā)與更簡單的單獨調試,而ARouter的出現就是讓組件間、模塊間是實現完全的獨立。ARouter主要解決組件間、模塊間的界面跳轉問題。
    2021-06-06
  • 詳解dex優(yōu)化對Arouter查找路徑的影響

    詳解dex優(yōu)化對Arouter查找路徑的影響

    dex簡單說就是優(yōu)化后的android版.exe。每個apk安裝包里都有。相對于PC上的java虛擬機能運行.class,android上的Davlik虛擬機能運行.dex。本文將著重介紹dex優(yōu)化對Arouter查找路徑的影響
    2021-06-06
  • Android實現自定義驗證碼輸入框效果(實例代碼)

    Android實現自定義驗證碼輸入框效果(實例代碼)

    這篇文章主要介紹了Android實現自定義驗證碼輸入框效果,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-01-01
  • Android DownloadMananger管理器實現下載圖片功能

    Android DownloadMananger管理器實現下載圖片功能

    Android DownloadMananger類似于下載隊列,管理所有當前正在下載或者等待下載的項目,他可以維持HTTP鏈接,并且在隊列中的下載項目一旦失敗,還能自動重新下載
    2023-01-01

最新評論

上栗县| 吕梁市| 邓州市| 白银市| 上林县| 台州市| 林周县| 通江县| 曲靖市| 绵竹市| 西贡区| 曲靖市| 台中县| 宜州市| 依安县| 玛纳斯县| 南靖县| 崇明县| 神木县| 东阿县| 徐闻县| 长白| 四会市| 富民县| 林芝县| 延寿县| 河南省| 博乐市| 东兴市| 白玉县| 富蕴县| 泸溪县| 马尔康县| 台东县| 冀州市| 栾川县| 文安县| 门源| 桑植县| 莱州市| 海城市|