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

Android應(yīng)用中使用TabHost組件繼承TabActivity的布局方法

 更新時間:2016年04月12日 15:36:57   作者:chenzheng_java  
這篇文章主要介紹了Android應(yīng)用中使用TabHost組件繼承TabActivity的布局方法,文中分別介紹了以Activity和以布局文件進行布局的方式,需要的朋友可以參考下

繼承TabActivity并以activity布局
先查看下最終效果圖:

2016412153332640.gif (388×256)

再看下代碼結(jié)構(gòu):

2016412153402122.gif (251×551)

其中black.gif顧名思義就是一個黑背景圖片,grey.gif就是一張灰色的背景圖片

然后直接上代碼:
ArtistActivity.java

package cn.com.tagview; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class ArtistActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView textView = new TextView(this); 
    // 該文檔將會作為標簽的內(nèi)容進行顯示 
    textView.setText("藝術(shù)內(nèi)容"); 
    setContentView(textView); 
     
  } 
} 

MusicActivity.java

package cn.com.tagview; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class MusicActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView textView = new TextView(this); 
    // 該文檔將會作為標簽的內(nèi)容進行顯示 
    textView.setText("音樂內(nèi)容"); 
    setContentView(textView); 
  } 
} 

SportActivity.java

package cn.com.tagview; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 
 
public class SportActivity extends Activity { 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    TextView textView = new TextView(this); 
    // 該文檔將會作為標簽的內(nèi)容進行顯示 
    textView.setText("運動內(nèi)容"); 
    setContentView(textView); 
     
  } 
} 

ArtistActivity.java  MusicActivity.java  SportActivity.java三個activity是用做標簽內(nèi)容的activity。即當用戶點擊相應(yīng)的標簽時,下邊會顯示相應(yīng)的activity內(nèi)容。

ic_tab.xml代碼

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

這里一定要注意ic_tab.xml文件的位置,是放在res/drawable文件夾下的。有些朋友說怎么沒有這個文件夾啊,實際上大家看到了我將它放在了drawable-hdpi中了,實際上drawable-hdpi、drawable-ldpi、drawable-mdpi三個文件夾都屬于drawable文件夾的哦。該文件它規(guī)定了,當標簽獲得焦點和失去焦點時,標簽上顯示什么圖片。

例如本例中,就是當state_selected="true"(當標簽被選中時),顯示@drawable/grey指定的資源圖片。當未被選中時,顯示@drawable/black指定的資源圖片。

tagView.java代碼:

package cn.com.tagview; 
 
import android.app.TabActivity; 
import android.content.Intent; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.widget.TabHost; 
 
/** 
 * @author chenzheng_Java 
 * @description 注意,該類一定要繼承TabActivity 
 */ 
public class TagView extends TabActivity { 
  @Override 
  public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // setContentView(R.layout.main); 
    // android代碼中訪問application資源的一個類 
    Resources resources = getResources(); 
    // 獲取當前activity的標簽,該方法的實現(xiàn)中已經(jīng)執(zhí)行了setContentView(com.android.internal.R.layout.tab_content); 
    TabHost tabHost = getTabHost(); 
    // 每一個標簽項 
    TabHost.TabSpec spec; 
 
    // 聲明一個意圖,該意圖告訴我們,下一個跳轉(zhuǎn)到的activity是ArtistActivity。 
    Intent intent = new Intent(this, ArtistActivity.class); 
    /** 
     * tabHost.newTabSpec("artist")創(chuàng)建一個標簽項,其中artist為它的標簽標識符,相當于jsp頁面標簽的name屬性 
     * setIndicator("藝術(shù)標簽",resources.getDrawable(R.drawable.ic_tab))設(shè)置標簽顯示文本以及標簽上的圖標(該圖標并不是一個圖片,而是一個xml文件哦) 
     * setContent(intent)為當前標簽指定一個意圖 
     * tabHost.addTab(spec); 將標簽項添加到標簽中 
     */ 
    spec = tabHost.newTabSpec("artist").setIndicator("藝術(shù)標簽", 
        resources.getDrawable(R.drawable.ic_tab)).setContent(intent); 
    tabHost.addTab(spec); 
 
    Intent intent2 = new Intent(this, MusicActivity.class); 
    spec = tabHost.newTabSpec("music").setIndicator("音樂標簽", 
        resources.getDrawable(R.drawable.ic_tab)).setContent(intent2); 
    tabHost.addTab(spec); 
 
    Intent intent3 = new Intent(this, SportActivity.class); 
    spec = tabHost.newTabSpec("sport").setIndicator("體育標簽", 
        resources.getDrawable(R.drawable.ic_tab)).setContent(intent3); 
    tabHost.addTab(spec); 
 
    // tabHost.setCurrentTabByTag("music");設(shè)置第一次打開時默認顯示的標簽,該參數(shù)與tabHost.newTabSpec("music")的參數(shù)相同 
    tabHost.setCurrentTab(1);//設(shè)置第一次打開時默認顯示的標簽,參數(shù)代表其添加到標簽中的順序,位置是從0開始的哦。 
 
  } 
} 

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
   package="cn.com.tagview" 
   android:versionCode="1" 
   android:versionName="1.0"> 
  <uses-sdk android:minSdkVersion="8" /> 
 
  <application android:icon="@drawable/icon" android:label="@string/app_name"> 
    <!-- android:theme="@android:style/Theme.NoTitleBar" 的意思是將系統(tǒng)默認的tag標簽去掉,為咱們自己的標簽空出位置--> 
    <activity android:name=".TagView" 
         android:label="@string/app_name" 
         android:theme="@android:style/Theme.NoTitleBar" 
         > 
      <intent-filter> 
        <action android:name="android.intent.action.MAIN" /> 
        <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
    </activity> 
    <!-- 在主配置文件中聲明用于標簽切換的3個activity,記住此處一定要聲明,否則會出錯 
      android:name="ArtistActivity"里面ArtistActivity前面是否有.都可以,你只需要保證該類是在manifest標簽下package屬性的包中即可。 
     --> 
    <activity android:name="ArtistActivity" android:label="@string/app_name"></activity>  
    <activity android:name="MusicActivity" android:label="@string/app_name"></activity>  
    <activity android:name="SportActivity" android:label="@string/app_name"></activity> 
 
  </application> 
</manifest> 

一切都弄好之后,運行,就出現(xiàn)了最終效果。這里要注意,main.xml是一直都沒有用到的哦。

廢話連篇:

其實,利用TabHost布局與ListView有很多相似之處,系統(tǒng)也同樣為他們提供了幫助類,TabHost-TabActivity  ListView-ListActivity .當我們的activity集成了這些類之后,一般在里面我們只需要整理綁定下數(shù)據(jù)就可以。
再次聲明一下,代碼中是存在setContentView方法的調(diào)用的,只不過因為我們集成了TabActivity,TabActivity的getTabHost方法中已經(jīng)進行了實現(xiàn)而已。對用戶隱藏了,并不代表沒有。
項目中為了簡單易懂,我們只是在每個標簽的內(nèi)容部分添加了一個文本。實際上,我們完全可以在里面添加圖片、視頻等等。只要在相應(yīng)的activity中實現(xiàn)就行了。我們可以看到,這種方式其實有很好的分層結(jié)構(gòu),activity與activity之間沒有太多耦合。
可能一直到現(xiàn)在,有些朋友對TabActivity和ListActivity這種實現(xiàn)都特別的別扭。我這里就簡單的說一下,實際上這其實是一種設(shè)計模式,模板模式。系統(tǒng)給你提供了一個實現(xiàn)了大部分內(nèi)容的模板,然后你通過繼承模板,去做修改(例如模板中有一個方法沒有任何實現(xiàn),你重寫該方法并對其進行具體實現(xiàn)),讓其符合你的要求。這就是模板模式的原理。

繼承TabActivity并以布局文件進行布局
然后再來看以XML布局文件進行布局的方法,先上效果圖:

2016412153506750.gif (364×347)

上面的是最終效果圖。
代碼結(jié)構(gòu)如下。

2016412153522899.gif (249×438)

main.xml代碼:

<?xml version="1.0" encoding="utf-8"?> 
<!-- 該布局文件定義了標簽的內(nèi)容部分,該布局文件一定要以FrameLayout為根元素 --> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  android:layout_width="fill_parent" android:layout_height="fill_parent"> 
  <!-- 第一個標簽內(nèi)容 --> 
  <LinearLayout android:id="@+id/widget_layout_Blue" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <EditText android:id="@+id/widget34" android:layout_width="fill_parent" 
      android:layout_height="wrap_content" android:text="EditText" 
      android:textSize="18sp"> 
    </EditText> 
    <Button android:id="@+id/widget30" android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="Button"> 
    </Button> 
  </LinearLayout> 
  <!-- 第二個標簽內(nèi)容 AnalogClock為鐘表組件--> 
  <LinearLayout android:id="@+id/widget_layout_red" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <AnalogClock android:id="@+id/widget36" 
      android:layout_width="wrap_content" android:layout_height="wrap_content"> 
    </AnalogClock> 
  </LinearLayout> 
  <!-- 第三個標簽內(nèi)容 RadioButton必須在RadioGroup中哦 --> 
  <LinearLayout android:id="@+id/widget_layout_green" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 
    <RadioGroup android:id="@+id/widget43" 
      android:layout_width="166px" android:layout_height="98px" 
      android:orientation="vertical"> 
      <RadioButton android:id="@+id/widget44" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="RadioButton"> 
      </RadioButton> 
      <RadioButton android:id="@+id/widget45" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" 
        android:text="RadioButton"> 
      </RadioButton> 
    </RadioGroup> 
  </LinearLayout> 
</FrameLayout> 

TagHostTest.java的代碼:

package cn.com.tagHost.test; 
 
import android.app.TabActivity; 
import android.graphics.Color; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.ViewGroup; 
import android.widget.TabHost; 
 
public class TagHostTest extends TabActivity { 
 
  private TabHost myTabhost; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    myTabhost = this.getTabHost(); 
 
    /** 
     * inflate(int resource, ViewGroup root, boolean attachToRoot) 
     * resource 很顯然是一個資源索引id 
     * 當attachToRoot為true時,root代表一個可放置于容器中的組件 
     * 當attachToRoot為false時,root僅代表一個存儲值的對象 
     * 該方法的意思是,將根據(jù)R.layout.main生成的標簽View,添加到由myTabhost.getTabContentView()獲得的父容器中 
     * LayoutInflater類的inflate方法中有如下片段 
     * if (root != null && attachToRoot) { 
            root.addView(temp, params); 
          } 
      其中temp是根據(jù)resource指定的資源生成的一個和標簽有關(guān)的view 
     */ 
    LayoutInflater.from(this).inflate(R.layout.main, 
        myTabhost.getTabContentView(), true); 
    myTabhost.setBackgroundColor(Color.argb(150, 22, 70, 150)); 
 
    myTabhost.addTab(myTabhost.newTabSpec("One") 
        .setIndicator("A").setContent(R.id.widget_layout_Blue)); 
 
    myTabhost.addTab(myTabhost.newTabSpec("Two") 
        .setIndicator("B", getResources().getDrawable(R.drawable.icon)) 
        .setContent(R.id.widget_layout_green)); 
 
    myTabhost.addTab(myTabhost.newTabSpec("Three") 
        .setIndicator("C", getResources().getDrawable(R.drawable.icon)) 
        .setContent(R.id.widget_layout_red)); 
  } 
} 

這種方法實現(xiàn)起來比較簡單,看看我們都做了些什么。
第一步:定義標簽內(nèi)容部分的布局文件,該布局文件必須以FrameLayout為根節(jié)點。
第二步:讓activity繼承TabActivity,然后實現(xiàn)自己的代碼。


相關(guān)文章

  • 詳解Android 折疊屏適配攻略

    詳解Android 折疊屏適配攻略

    這篇文章主要介紹了Android 折疊屏適配攻略,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • 基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題

    基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題

    這篇文章主要介紹了基于Android FileProvider 屬性配置詳解及FileProvider多節(jié)點問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android基于高德地圖完全自定義Marker的實現(xiàn)方法

    Android基于高德地圖完全自定義Marker的實現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Android基于高德地圖完全自定義Marker的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • Android實現(xiàn)文件上傳和下載倒計時功能的圓形進度條

    Android實現(xiàn)文件上傳和下載倒計時功能的圓形進度條

    這篇文章主要介紹了Android實現(xiàn)文件上傳和下載倒計時功能的圓形進度條,需要的朋友可以參考下
    2017-09-09
  • Android自定義scrollview實現(xiàn)回彈效果

    Android自定義scrollview實現(xiàn)回彈效果

    這篇文章主要為大家詳細介紹了Android自定義scrollview實現(xiàn)回彈效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android掃描和生成二維碼

    Android掃描和生成二維碼

    這篇文章主要為大家詳細介紹了Android掃描二維碼和生成二維碼,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-11-11
  • Android應(yīng)用自動更新功能實現(xiàn)的方法

    Android應(yīng)用自動更新功能實現(xiàn)的方法

    這篇文章主要為大家詳細介紹了Android應(yīng)用自動更新功能實現(xiàn)的方法,感興趣的小伙伴們可以參考一下
    2016-06-06
  • 解決Android Studio sdk emulator directory is missing問題

    解決Android Studio sdk emulator directory is missing問題

    這篇文章主要介紹了解決Android Studio sdk emulator directory is missing問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • android 自定義TabActivity的實例方法

    android 自定義TabActivity的實例方法

    系統(tǒng)自帶的TabActivity的效果不甚理想。開發(fā)中對TabActivity自定義可能有兩種:第一種:改變TAB行的位置,如放到頁面下方。第二種:對TabHost圖片的自定義
    2013-11-11
  • Android中通知欄跳動問題解決方法

    Android中通知欄跳動問題解決方法

    這篇文章主要介紹了Android中通知欄跳動問題解決方法,導(dǎo)致這個問題的原因是when這個屬性值,默認它是使用的系統(tǒng)當前時間,這就是導(dǎo)致跳動問題的原因,指定一個固定時間即可解決這個問題,需要的朋友可以參考下
    2015-01-01

最新評論

深水埗区| 全椒县| 崇仁县| 台北市| 怀远县| 宝应县| 封开县| 荣昌县| 澄江县| 克东县| 安达市| 公主岭市| 牡丹江市| 绥滨县| 灯塔市| 定襄县| 手游| 专栏| 耿马| 元江| 榆中县| 盐源县| 应城市| 宁陵县| 榆树市| 天水市| 峨山| 乐至县| 德兴市| 兴隆县| 永善县| 枣庄市| 吉林市| 萨迦县| 海门市| 三穗县| 海安县| 黑水县| 卢湾区| 南宁市| 桓仁|