欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

詳解Android應(yīng)用中使用TabHost組件進(jìn)行布局的基本方法

 更新時間:2016年04月12日 15:44:56   作者:chenzheng_java  
這篇文章主要介紹了Android應(yīng)用中使用TabHost組件進(jìn)行布局的基本方法,不繼承TabActivity并以最基本的布局文件方式進(jìn)行布局,需要的朋友可以參考下

TabHost布局文件

我們先來了解一下布局文件的基本內(nèi)容:
1. 根標(biāo)簽及id

設(shè)置Android自帶id : XML布局文件中, 可以使用 標(biāo)簽設(shè)置, 其中的id 需要引用 android的自帶id :

android:id=@android:id/tabhost ;

getHost()獲取前提 : 設(shè)置了該id之后, 在Activity界面可以使用 getHost(), 獲取這個TabHost 視圖對象;

示例 :

復(fù)制代碼 代碼如下:

<tabhost android:id="@android:id/tabhost" android:layout_height="match_parent" android:layout_width="match_parent"></tabhost>

2. TabWidget組件

選項(xiàng)卡切換 : 該組件是選項(xiàng)卡切換按鈕, 通過點(diǎn)擊該組件可以切換選項(xiàng)卡;

設(shè)置android自帶id : 這個組件的id要設(shè)置成android的自帶id : android:id=@android:id/tabs ;

TabHost必備組件 : 該組件與FrameLayout組件是TabHost組件中必備的兩個組件;

切換按鈕下方顯示 : 如果想要將按鈕放到下面, 可以將該組件定義在下面, 但是注意,FrameLayout要設(shè)置android:layout_widget = 1;

設(shè)置TabWidget大小 : 如果想要設(shè)置該按鈕組件的大小, 可以設(shè)置該組件與FrameLayout組件的權(quán)重;

示例 :

復(fù)制代碼 代碼如下:

<tabwidget android:id="@android:id/tabs" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal/"></tabwidget>

3. FrameLayout組件

組件作用 : 該組件中定義的子組件是TabHost中每個頁面顯示的選項(xiàng)卡, 可以將TabHost選項(xiàng)卡顯示的視圖定義在其中;

設(shè)置android自帶id : 這個組件的id要設(shè)置成android的自帶的id : android:id=@android:id/tabcontent ;

示例 :

復(fù)制代碼 代碼如下:

<framelayout android:id="@android:id/tabcontent" android:layout_height="fill_parent" android:layout_weight="1" android:layout_width="fill_parent"></framelayout>

示例

2016412154201842.gif (354×175)

上圖為最終效果圖
代碼結(jié)構(gòu)圖

2016412154240467.gif (221×432)

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/hometabs" 
 android:orientation="vertical" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
 <!-- TabHost必須包含一個 TabWidget和一個FrameLayout--> 
 <TabHost android:id="@+id/tabhost" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  > 
  <LinearLayout 
   android:orientation="vertical" 
   android:layout_width="fill_parent" 
   android:layout_height="fill_parent"> 
   <!-- TabWidget的id屬性必須為 @android:id/tabs-->    
   <TabWidget android:id="@android:id/tabs" 
    android:orientation="horizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
   </TabWidget> 
   <!-- FrameLayout的id屬性必須為 @android:id/tabcontent--> 
    <FrameLayout android:id="@android:id/tabcontent" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <TextView android:id="@+id/view1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
     <TextView android:id="@+id/view2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
     <TextView android:id="@+id/view3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent"/> 
    </FrameLayout> 
   
   </LinearLayout> 
 </TabHost> 
</LinearLayout> 

java代碼如下

package cn.com.tagHost.test; 
 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TabHost; 
import android.widget.TabWidget; 
 
public class TagHostTest2 extends Activity { 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
  // 獲取TabHost對象 
  TabHost tabHost = (TabHost) findViewById(R.id.tabhost); 
  // 如果沒有繼承TabActivity時,通過該種方法加載啟動tabHost 
  tabHost.setup(); 
  tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一個標(biāo)簽", 
    getResources().getDrawable(R.drawable.icon)).setContent( 
    R.id.view1)); 
 
  tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三個標(biāo)簽") 
    .setContent(R.id.view3)); 
 
  tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二個標(biāo)簽") 
    .setContent(R.id.view2)); 
 } 
} 

運(yùn)行得到正確的結(jié)果。
廢話連篇:這里需要注意的是
第一:布局文件的格式。以及TabWidget和FrameLayout的id屬性值。
第二:TabWidget代表的是標(biāo)簽部分,F(xiàn)rameLayout代表的點(diǎn)擊標(biāo)簽后看到的內(nèi)容部分。FrameLayout里面聲明的組件意為具備成為標(biāo)簽內(nèi)容的資格,具體的還要在代碼中具體指定。
你是否也想要這種結(jié)果呢。讓標(biāo)簽在下部分顯示

2016412154317508.gif (362×516)

那么你只需要給main.xml進(jìn)行下布局修改就可以了。

main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:id="@+id/hometabs" android:orientation="vertical" 
 android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 <!-- TabHost必須包含一個 TabWidget和一個FrameLayout--> 
 <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" 
  android:layout_height="wrap_content"> 
  <LinearLayout android:orientation="vertical" 
   android:layout_width="fill_parent" android:layout_height="fill_parent"> 
 
   <!-- FrameLayout的id屬性必須為 @android:id/tabcontent--> 
   <FrameLayout android:id="@android:id/tabcontent" 
    android:layout_width="fill_parent" android:layout_height="fill_parent"> 
    <TextView android:id="@+id/view1" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:text="hello baby!" 
     /> 
    <TextView android:id="@+id/view2" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
    <TextView android:id="@+id/view3" android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 
   </FrameLayout> 
   <RelativeLayout android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
 
    <!-- TabWidget的id屬性必須為 @android:id/tabs--> 
    <TabWidget android:id="@android:id/tabs" 
     android:orientation="horizontal" android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:paddingBottom="0dp" 
     > 
    </TabWidget> 
   </RelativeLayout> 
  </LinearLayout> 
 </TabHost> 
</LinearLayout> 

為了讓標(biāo)簽和父容器底部持平,我們使用了android:layout_alignParentBottom="true",該屬性只有在RelativeLayout布局中才會存在哦、這也是為什么我們將tabWidget放入一個RelativeLayout中的原因。
此外,在lineaerLayout布局中,TabWidget和FrameLayout的位置可是調(diào)換了哦。

相關(guān)文章

  • Android自定義圓形倒計時進(jìn)度條

    Android自定義圓形倒計時進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android自定義圓形倒計時進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • Android自定義View實(shí)現(xiàn)圓環(huán)交替效果

    Android自定義View實(shí)現(xiàn)圓環(huán)交替效果

    這篇文章給大家介紹如何基于Android自定義View實(shí)現(xiàn)圓環(huán)交替的效果,實(shí)現(xiàn)后效果很贊,有需要的小伙伴們可以參考借鑒。
    2016-08-08
  • Android this與Activity.this的區(qū)別

    Android this與Activity.this的區(qū)別

    這篇文章主要介紹了 Android this與Activity.this的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2016-09-09
  • Kotlin fun函數(shù)使用方法

    Kotlin fun函數(shù)使用方法

    函數(shù)是執(zhí)行特定任務(wù)的一組相互關(guān)聯(lián)的代碼塊。函數(shù)用于將程序分解為不同的子模塊。它使代碼可重用,并使程序更易于管理,這篇文章主要介紹了Kotlin fun函數(shù)使用方法
    2022-12-12
  • android實(shí)現(xiàn)圖片裁剪的兩種方法

    android實(shí)現(xiàn)圖片裁剪的兩種方法

    這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)圖片裁剪的兩種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • 一文徹底搞懂Kotlin中的協(xié)程

    一文徹底搞懂Kotlin中的協(xié)程

    這篇文章主要給大家介紹了Kotlin中協(xié)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Android下SDL2實(shí)現(xiàn)五子棋游戲

    Android下SDL2實(shí)現(xiàn)五子棋游戲

    這篇文章主要為大家詳細(xì)介紹了Android下SDL2實(shí)現(xiàn)五子棋游戲的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • Android抽屜布局DrawerLayout的簡單使用

    Android抽屜布局DrawerLayout的簡單使用

    這篇文章主要為大家詳細(xì)介紹了Android抽屜布局DrawerLayout的簡單使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • Android 自定義可拖拽View界面渲染刷新后不會自動回到起始位置

    Android 自定義可拖拽View界面渲染刷新后不會自動回到起始位置

    這篇文章主要介紹了Android 自定義可拖拽View界面渲染刷新后不會自動回到起始位置的實(shí)現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-02-02
  • Android使用百度語音識別的示例代碼

    Android使用百度語音識別的示例代碼

    本篇文章主要介紹了Android使用百度語音識別的示例代碼,詳細(xì)介紹了使用百度語音識別,完成語音識別的功能,有興趣的可以了解一下。
    2017-02-02

最新評論