国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

詳解Android TabHost的多種實現方法 附源碼下載

2019-12-12 06:19:38
字體:
來源:轉載
供稿:網友

最近仔細研究了下TabHost,主要是為了實現微信底部導航欄的功能,最后也給出一個文章鏈接,大家不要著急

正文:

TabHost的實現分為兩種,一個是不繼承TabActivity,一個是繼承自TabActivity;當然了選用繼承自TabActivity的話就相對容易一些,下面來看看分別是怎樣來實現的吧。

方法一、定義tabhost:不用繼承TabActivity
 1、布局文件:activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  tools:context=".MainActivity" >  <Button  android:id="@+id/button1"  android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:text="Button" />  <TabHost  android:id="@+id/tabhost"  android:layout_width="match_parent"  android:layout_height="wrap_content">   <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" >   </TabWidget>    <FrameLayout   android:id="@android:id/tabcontent"   android:layout_width="match_parent"   android:layout_height="match_parent" >    <!-- 第一個tab的布局 -->   <LinearLayout    android:id="@+id/tab1"    android:layout_width="match_parent"    android:layout_height="match_parent" >     <TextView    android:id="@+id/textView1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="林炳東" />    </LinearLayout>    <!-- 第二個tab的布局 -->   <LinearLayout    android:id="@+id/tab2"    android:layout_width="match_parent"    android:layout_height="match_parent" >     <TextView    android:id="@+id/textView2"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="張小媛" />    </LinearLayout>    <!-- 第三個tab的布局 -->   <LinearLayout    android:id="@+id/tab3"    android:layout_width="match_parent"    android:layout_height="match_parent" >     <TextView    android:id="@+id/textView3"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="馬貝貝" />    </LinearLayout>   </FrameLayout>  </LinearLayout>  </TabHost>  </LinearLayout> 

2、JAVA代碼

public class MainActivity extends Activity {   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);    TabHost th=(TabHost)findViewById(R.id.tabhost);  th.setup();  //初始化TabHost容器    //在TabHost創建標簽,然后設置:標題/圖標/標簽頁布局  th.addTab(th.newTabSpec("tab1").setIndicator("標簽1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));  th.addTab(th.newTabSpec("tab2").setIndicator("標簽2",null).setContent(R.id.tab2));  th.addTab(th.newTabSpec("tab3").setIndicator("標簽3",null).setContent(R.id.tab3));   //上面的null可以為getResources().getDrawable(R.drawable.圖片名)設置圖標   } } 

效果圖:

此例源碼地址:Demo1

方法二、Tab的內容分開:不用繼承TabActivity
1、第一個tab的XML布局文件,tab1.xml:

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/LinearLayout01"  android:layout_width="wrap_content"  android:layout_height="wrap_content">  <TextView   android:text="我是標簽1的內容喔"   android:id="@+id/TextView01"   android:layout_width="wrap_content"   android:layout_height="wrap_content">  </TextView>  </LinearLayout> 

2、第二個tab的XML布局文件,tab2.xml:

<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:id="@+id/LinearLayout02"  android:layout_width="wrap_content"  android:layout_height="wrap_content">   <TextView android:text="標簽2"    android:id="@+id/TextView01"    android:layout_width="wrap_content"    android:layout_height="wrap_content" /> </LinearLayout> 

3、主布局文件,activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:orientation="vertical" >   <TabHost  android:id="@+id/tabhost"   android:layout_width="match_parent"  android:layout_height="match_parent" >   <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" >   </TabWidget>    <FrameLayout   android:id="@android:id/tabcontent"   android:layout_width="match_parent"   android:layout_height="match_parent" >      </FrameLayout>  </LinearLayout>  </TabHost>  </LinearLayout> 

4、JAVA代碼:

public class MainActivity extends Activity {   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);    TabHost m = (TabHost)findViewById(R.id.tabhost);  m.setup();    LayoutInflater i=LayoutInflater.from(this);  i.inflate(R.layout.tab1, m.getTabContentView());  i.inflate(R.layout.tab2, m.getTabContentView());//動態載入XML,而不需要Activity    m.addTab(m.newTabSpec("tab1").setIndicator("標簽1").setContent(R.id.LinearLayout01));   m.addTab(m.newTabSpec("tab2").setIndicator("標簽2").setContent(R.id.LinearLayout02));   } } 

效果圖:


此例源碼地址:Demo2

方法三、繼承自TabActivity
1、主布局文件,activity_main.xml:

<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="fill_parent">   <!-- 第一個布局 -->  <LinearLayout  android:id="@+id/view1"  android:layout_width="match_parent"  android:layout_height="match_parent" >  <TextView   android:id="@+id/textView1"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="張小媛" />  </LinearLayout>   <!-- 第二個布局 -->  <LinearLayout  android:id="@+id/view2"  android:layout_width="match_parent"  android:layout_height="match_parent" >    <TextView   android:id="@+id/textView2"   android:layout_width="wrap_content"   android:layout_height="wrap_content"   android:text="馬貝貝" />  </LinearLayout>   <!-- 第三個布局 -->  <TextView android:id="@+id/view3"  android:background="#00ff00"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  android:text="Tab3"/>  </FrameLayout> 

2、JAVA代碼:
先將派生自Activity改為TabActivity,然后代碼如下: 

public class MainActivity extends TabActivity {   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setTitle("TabDemoActivity");  TabHost tabHost = getTabHost();  LayoutInflater.from(this).inflate(R.layout.activity_main,   tabHost.getTabContentView(), true);  tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher))   .setContent(R.id.view1));  tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2")   .setContent(R.id.view2));  tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3")   .setContent(R.id.view3));       //標簽切換事件處理,setOnTabChangedListener  tabHost.setOnTabChangedListener(new OnTabChangeListener(){   @Override   public void onTabChanged(String tabId) {   if (tabId.equals("tab1")) { //第一個標簽   }   if (tabId.equals("tab2")) { //第二個標簽   }   if (tabId.equals("tab3")) { //第三個標簽   }   }   });      }  } 

效果圖:

 

此例源碼地址:Demo3

四、實現微信底部導航欄

效果:

 

參看:Android仿微信底部菜單欄功能顯示未讀消息數量

原文地址:http://blog.csdn.net/harvic880925/article/details/17120325

以上就是本文的全部內容,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 蒙城县| 通化县| 泊头市| 新干县| 定州市| 台江县| 淳安县| 禹城市| 阿图什市| 鄂州市| 札达县| 抚顺市| 铁岭市| 德保县| 观塘区| 高阳县| 奉节县| 乌鲁木齐市| 阜南县| 乐陵市| 巴马| 玛曲县| 红河县| 烟台市| 峨边| 枣阳市| 萨迦县| 张家川| 呼玛县| 澳门| 盘锦市| 景德镇市| 丰原市| 那坡县| 宣化县| 临城县| 齐河县| 镇巴县| 宁城县| 布拖县| 罗甸县|